Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从列表中获取特殊成员列表(包括MyClass中的成员)<;MyClass>;?_C#_Reflection - Fatal编程技术网

C# 如何从列表中获取特殊成员列表(包括MyClass中的成员)<;MyClass>;?

C# 如何从列表中获取特殊成员列表(包括MyClass中的成员)<;MyClass>;?,c#,reflection,C#,Reflection,我有下一个代码: public class MyClass { object field1; object field2; } /////// List<MyClass> lst = new List<MyClass>(); GetLstMember(lst, "field1"); /////// List<object> GetLstMember(List<Object> lst, string memberName) {

我有下一个代码:

public class MyClass
{
   object field1;
   object field2;
}

///////
List<MyClass> lst = new List<MyClass>();
GetLstMember(lst, "field1");

///////
List<object> GetLstMember(List<Object> lst, string memberName)
{
      List<object> rezult = new List<object>();

      for(int i=0; i<lst.Count; i++)
      {
           rezult.Add(lst[i].GetType().InvokeMember(
               memberName, 
               BindingFlag.GetProperty, 
               null,
               lst[i],
               null);
      }

      return rezult;
} 
好的,我理解使用反射。这些代码与以前的代码在性能上是否存在差异:

List<object> GetLstMember(List<Object> lst, string memberName) 
{ 
     List<object> rezult = new List<object>(); 
     for (int i = 0; i < lst.Count; i++) 
       rezult.Add(lst[i].GetType().GetProperty(memberName).GetValue(lst[i],null)**); 
     return rezult; 
}
List GetLstMember(List lst,string memberName)
{ 
List rezult=新列表();
对于(int i=0;i
这是唯一的方法;这就是反思的目的

即使使用绑定列表,它也会使用反射。

此外,BindingList实际上无法做到这一点。

如果您在.net 4上运行,则可以使用dynamic关键字

这样的办法应该行得通

public class MyClass {
    object field1;
    object field2;
}

///////
List<MyClass> lst = new List<MyClass>();
GetLstMember(lst);
///////
List<object> GetLstMember(List<dynamic> lst, string memberName)
{
      List<object> rezult=new List<object>();
      for(int i=0;i<lst.Count;i++){
            switch(memberName){
                case "field1":
                    rezult.Add(lst[i].field1);
                break;
                case "field2":
                    rezult.Add(lst[i].field2);
                break;          
            }
        }
    return rezult;
} 
公共类MyClass{
对象字段1;
对象字段2;
}
///////
List lst=新列表();
GETLST成员(lst);
///////
List GetLstMember(List lst,string memberName)
{
List rezult=新列表();

对于(inti=0;iSo)而言,这两种方法的性能是否存在差异

lst[i].GetType().GetProperty(memberName).GetValue(lst[i],null)

lst[i].GetType().InvokeMember(memberName, BindingFlag.GetProperty, null, lst[i], null)

这是一个非常糟糕的方法。使用
字典而不是“MyClass”,这取决于你想做什么?你的字段名必须是字符串吗?如果不是,你可以使用LINQ:
data=lst.Select(m=>m.field1)。ToList()
为什么不使用反射?如果是因为性能,我们可以讨论一些抵消成本的方法。如果是因为反射看起来有点凌乱,可以将其重新分解以更加清晰。我想可能存在针对此类内容的特殊类。是的,它被称为反射。
lst[i].GetType().GetProperty(memberName).GetValue(lst[i],null)

lst[i].GetType().InvokeMember(memberName, BindingFlag.GetProperty, null, lst[i], null)