Is it possible to convert a non-nullable value type known only at runtime to nullable? In other words:
public Type GetNullableType(Type t)
{
if (t.IsValueType)
{
return typeof(Nullable<t>);
}
else
{
throw new ArgumentException();
}
}
Obviously the return line gives an error. Is there a way to do this? The Type.MakeGenericType method seems promising, but I have no idea how to get a unspecified generic Type object representing Nullable<T>. Any ideas?
you want
typeof(Nullable<>).MakeGenericType(t)Note:
Nullable<>without any supplied arguments is the unbound generic type; for more complex examples, you would add commas to suit – i.e.KeyValuePair<,>,Tuple<,,,>etc.