You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Interop/Nullables.cs

40 lines
922 B

using System.Linq.Expressions;
using System.Reflection;
namespace Connected.Interop
{
public static class Nullables
{
public static bool IsNullableType(this Type type)
{
return type is not null && type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
public static bool IsNullAssignable(this Type type)
{
return !type.GetTypeInfo().IsValueType || type.IsNullableType();
}
public static Type GetNonNullableType(this Type type)
{
if (type.IsNullableType())
return type.GetTypeInfo().GenericTypeArguments[0];
return type;
}
public static Type GetNullAssignableType(this Type type)
{
if (!type.IsNullAssignable())
return typeof(Nullable<>).MakeGenericType(type);
return type;
}
public static ConstantExpression GetNullConstant(this Type type)
{
return Expression.Constant(null, type.GetNullAssignableType());
}
}
}