ObjectExtensions
Various extension methods for reference types.
using DotNext.Runtime;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace DotNext
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class ObjectExtensions
{
internal static bool IsNull(object obj)
{
return obj == null;
}
internal static bool IsNotNull(object obj)
{
return obj != null;
}
internal static bool IsTypeOf<T>(object obj)
{
return obj is T;
}
internal static TOutput Identity<TInput, TOutput>(TInput input) where TInput : TOutput
{
return (TOutput)(object)input;
}
[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 unsafe static bool IsOneOf<T>(this T value, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})] params T[] values) where T : class
{
for (IntPtr intPtr = (IntPtr)0; (long)intPtr < (long)Intrinsics.GetLength(values); intPtr = (IntPtr)(void*)((long)intPtr + 1)) {
if (object.Equals(values[(long)intPtr], value))
return true;
}
return false;
}
internal static bool IsContravariant(object obj, Type type)
{
return obj?.GetType().IsAssignableFrom(type) ?? false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T As<[System.Runtime.CompilerServices.Nullable(2)] T>(this T obj) where T : class
{
return obj;
}
}
}