比如 类A
有int型的字段 CInt
string型的字段 CString
DateTime类型的字段 CDatetime如何使用反射,让CInt =0;CString =string.empty;CDatetime=Datetime.now我目前实现的代码PropertyInfo[] properties = this.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
Type propertyType = property.PropertyType;
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments()[0];
}
if(propertyType.Equals(typeof(int)))
{
property.SetValue(this, default(int), null);
} }this表示这个类A就是setValue那里我只能判断 然后分别给默认值,但我不想这样写N多if-else。请问各位大虾有没有好的办法
有int型的字段 CInt
string型的字段 CString
DateTime类型的字段 CDatetime如何使用反射,让CInt =0;CString =string.empty;CDatetime=Datetime.now我目前实现的代码PropertyInfo[] properties = this.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
Type propertyType = property.PropertyType;
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments()[0];
}
if(propertyType.Equals(typeof(int)))
{
property.SetValue(this, default(int), null);
} }this表示这个类A就是setValue那里我只能判断 然后分别给默认值,但我不想这样写N多if-else。请问各位大虾有没有好的办法
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货