This might help:
http://weblogs.asp.net/grantbarrington/archive/2009/03/17/using-reflection-to-determine-whether-an-type-is-nullable-and-get-the-underlying-type.aspx[
^]
There is some information on getting Nullable on msdn (
http://msdn.microsoft.com/en-us/library/ms366789.aspx[
^]), but only for types. You cannot use the
object.GetType()
to determine if something is Nullable because if you do a
GetType()
on an
int?
you just get the result of int. This code will work with
typeof()
(ie
typeof(int?)
):
public static class Extensions
{
public static bool IsNullable(this Type type)
{
return type.IsGenericType
&& (type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
}
This code will work with generics;
public class x<T>
{
public x()
{
var x = typeof(T).IsNullable();
}
}