Andrew Robinson <ne****@nospam.nospamwrote:
I am guessing there is a simple solution but given a type T, how can I check
for nullability?
how can I accomplish the following?
bool nullable = typeof(int).IsNullable; // false
bool nullable = typeof(int?).IsNullable; // true
bool nullable = typeof(string).IsNullable; // true
using System;
class Test
{
static void Main()
{
Console.WriteLine (IsNullable(typeof(int)));
Console.WriteLine (IsNullable(typeof(int?)));
Console.WriteLine (IsNullable(typeof(string)));
}
static bool IsNullable (Type t)
{
return !t.IsValueType ||
(t.IsGenericType &&
t.GetGenericTypeDefinition()==typeof(Nullable<>));
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too