2 回答
TA贡献1847条经验 获得超7个赞
如果您只需要值为 1 的属性和值,则可以将 GetValue 方法与 LINQ 结合使用:
var incidentImageModel = new IncidentImageModel();
PropertyInfo[] properties = incidentImageModel.GetType().GetProperties();
var result = from property in properties
let nameAndValue = new { property.Name, Value = (int)property.GetValue(incidentImageModel) }
where nameAndValue.Value == 1
select nameAndValue;
TA贡献1810条经验 获得超5个赞
这是您的固定代码:
PropertyInfo[] properties = incident.GetType().GetProperties();
for (int i = 0; i < properties.Length; i++)
{
var pName = properties[i].Name;
var pValue = properties[i].GetValue(incident);
Console.WriteLine($"{pName} = {pValue}");
}
- 2 回答
- 0 关注
- 120 浏览
添加回答
举报