我尝试动态设置 Nullable<> 属性。
我得到我的财产前:
PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int
我想通过反射来设置我的属性,比如
property.SetValue(class,"1256",null);
当我的属性是 Nullable<> Generic 时,它不起作用。所以我试图找到一种方法来设置我的财产。
要知道我执行的 nullable<> 属性的类型
Nullable.GetUnderlyingType(property.PropertyType)
任何的想法 ?
我尝试使用创建我的 Nullable<> 属性的实例
var nullVar = Activator.CreateInstance(typeof(Nullable<>).MakeGenericType(new Type[] { Nullable.GetUnderlyingType(property.PropertyType) }));
但 nullVar 始终为 Null