我有以下几点:的System.Reflection无法获得属性值
List<Agenda> Timetable;
public class Agenda
{
public Object item; //item can be of any object type but has common properties
}
class MasterItem
{
public long ID;
}
class item1:MasterItem { //properties and methods here }
class item2:MasterItem { //properties and methods here }
在代码的开始,我有我添加使用
item1 sItem = new item1() { //Initialize properties with values }
Timetable.Add(new Agenda {item = sItem);
在这里我要项的列表获得ID为12的项目的议程。我试过使用
object x = Timetable.Find(delegate (Agenda a)
{
System.Reflection.PropertyInfo pinfo = a.item.GetType().GetProperties().Single(pi => pi.Name == "ID"); //returned Sequence contains no matching element
return ....
}
它为什么会返回错误消息“序列包含没有匹配的元素”?
我也试过
a.item.GetType().GetProperty("ID")
但它返回“未设置为一个对象的实例对象引用”。它找不到该ID。
有趣的是,不要从谷歌搜索得到很多...
这有助于很多。谢谢! – cSharpDev