ObjectExtensions
Various extension methods for reference types.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class ObjectExtensions
{
[System.Runtime.CompilerServices.NullableContext(2)]
internal static bool IsNull(object obj)
{
return obj == null;
}
[System.Runtime.CompilerServices.NullableContext(2)]
internal static bool IsNotNull(object obj)
{
return obj != null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UserDataStorage GetUserData<T>(this T obj) where T : class
{
return new UserDataStorage(obj);
}
public static bool IsOneOf<T>(this T value, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})] IEnumerable<T> values) where T : class
{
foreach (T value2 in values) {
if (object.Equals(value, value2))
return true;
}
return false;
}
public static bool IsOneOf<T>(this T value, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})] params T[] values) where T : class
{
return IsOneOf(value, (IEnumerable<T>)values);
}
public static void Decompose<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TResult1, [System.Runtime.CompilerServices.Nullable(2)] TResult2>(this T obj, Func<T, TResult1> decomposer1, Func<T, TResult2> decomposer2, out TResult1 result1, out TResult2 result2) where T : class
{
ValueFunc<T, TResult1> decomposer3 = new ValueFunc<T, TResult1>(decomposer1, true);
ValueFunc<T, TResult2> decomposer4 = new ValueFunc<T, TResult2>(decomposer2, true);
obj.Decompose(ref decomposer3, ref decomposer4, out result1, out result2);
}
public static void Decompose<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TResult1, [System.Runtime.CompilerServices.Nullable(2)] TResult2>(this T obj, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] ref ValueFunc<T, TResult1> decomposer1, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] ref ValueFunc<T, TResult2> decomposer2, out TResult1 result1, out TResult2 result2) where T : class
{
result1 = decomposer1.Invoke(obj);
result2 = decomposer2.Invoke(obj);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})]
public static (TResult1, TResult2) Decompose<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TResult1, [System.Runtime.CompilerServices.Nullable(2)] TResult2>(this T obj, Func<T, TResult1> decomposer1, Func<T, TResult2> decomposer2) where T : class
{
ValueFunc<T, TResult1> decomposer3 = new ValueFunc<T, TResult1>(decomposer1, true);
ValueFunc<T, TResult2> decomposer4 = new ValueFunc<T, TResult2>(decomposer2, true);
return obj.Decompose(ref decomposer3, ref decomposer4);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})]
public static (TResult1, TResult2) Decompose<T, TResult1, TResult2>([System.Runtime.CompilerServices.Nullable(1)] this T obj, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] ref ValueFunc<T, TResult1> decomposer1, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] ref ValueFunc<T, TResult2> decomposer2) where T : class
{
(TResult1, TResult2) result = default((TResult1, TResult2));
obj.Decompose(ref decomposer1, ref decomposer2, out result.Item1, out result.Item2);
return result;
}
internal static bool IsContravariant([System.Runtime.CompilerServices.Nullable(2)] object obj, Type type)
{
return obj?.GetType().IsAssignableFrom(type) ?? false;
}
}
}