Person item=new Person();
//遍历类中属性
foreach (System.Reflection.PropertyInfo info in item.GetType().GetProperties())
{
//判断类属性为string类型
if (info.PropertyType.Name == "String") {
//通过属性名称获取属性的值
var value = item.GetType().GetProperty(info.Name).GetValue(item, null);
if (value == null)
{
//为属性重新赋值
info.SetValue(item, "-", null);
}
}
}
C#遍历类属性并赋值
于 2023-07-05 10:24:54 首次发布