using System.Reflection;
using System.ComponentModel;
List<object> listZy = new List<object>();
var zyitem = new
{
xi = xi,
zy = zy,
unitid = "",
unitname = ""
};
listZy.Add(zyitem);
bool ishave=false;
foreach (var item in listZy)
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(item);
PropertyDescriptor pdID = pdc.Find("xi", true);
var x = pdID.GetValue(item);
PropertyDescriptor pdName = pdc.Find("zy", true);
var z = pdName.GetValue(item);
if (x.ToString() == xi && z.ToString() == zy)
{
ishave = true;
break;
}
}
本文介绍了一种使用C#中的反射技术来遍历并检查列表中对象属性的方法。通过PropertyDescriptorCollection获取对象的所有属性,并利用Find方法定位特定属性进行值比较。

720

被折叠的 条评论
为什么被折叠?



