1

实现此方法的最佳方法是什么:

Type GetNullableTypeFromType(Type tp);

以便

Type tpString = GetNullableTypeFromType(typeof(string));   // Returns typeof(string)
Type tpInt = GetNullableTypeFromType(typeof(int));   // Returns typeof(int?)
4

1 回答 1

10
public static Type GetNullableType(Type t)
{
    if (t.IsValueType && (Nullable.GetUnderlyingType(t) == null)) 
        return typeof(Nullable<>).MakeGenericType(t); 
    else
        return t;
}
于 2012-05-24T21:50:43.107 回答