1、遍历方法
public static void ForeachClass<T>(T model)
{
Type t = model.GetType();
PropertyInfo[] PropertyList = t.GetProperties();
foreach (PropertyInfo item in PropertyList)
{
//属性名
string name = item.Name;
//属性值
object value = item.GetValue(model, null);
//属性值是否为NULL或者“”
if (value == null||string.IsNullOrEmpty(value.ToString()))
{
//对于字符型的属性,赋值。
if (item.PropertyType == typeof(string))
{
item.SetValue(model, "0",null);
}
}
}
}
2、调用
ForeachClass<类名称>(类实例);