C# 使用未知属性名从集合中获取值

C# 使用未知属性名从集合中获取值,c#,C#,首先,感谢您的关注和帮助 把故事一扫而光,重点如下 我收集了如下类型的汽车 public class Car { int a; int b; public Car() { a = b = 0; } public Car(int a, int b) { this.a = a; this.b = b; } public int A {

首先,感谢您的关注和帮助

把故事一扫而光,重点如下

我收集了如下类型的汽车

public class Car {
     int a;
     int b;

     public Car()
     {
         a = b = 0;
     }

     public Car(int a, int b)
     {
         this.a = a;
         this.b = b;
     }

     public int A {
         set { a = value; }
         get { return a; }
     }

     public int B {
         set { b = value; }
         get { return b; }
     }
}


ObservableCollection<Car> carColl = new ObservableCollection<Car>();
carColl.Add(new Car(10, 100));
carColl.Add(new Car(20, 200));
carColl.Add(new Car(30, 300));
请让我知道怎么做。。。
非常感谢

您必须使用反射:

var properties = TypeDescriptor.GetProperties(typeof(Car));
foreach (Car car in carColl)
{
    foreach (string propName in propertyNames)
    {
        Console.WriteLine(properties[propName].GetValue(car));
    }
}

如果您不熟悉反射:使用反射,您可以访问对象的元信息,例如确切的类型、属性名称、属性类型,否则这些信息将不可用,因为它们在编译过程中被删除。通过这些元信息,您可以访问对象,例如返回属性值或执行方法。

这是因为propName不是car类的属性。
您只能访问此类car.A,car.B中定义的属性

我认为很明显您不能调用car.propName 因为您的汽车构造函数不包含名为propName的字段,所以您可以调用A或B。 我也看不出汽车和你的清单之间的关系

在这种情况下,您的foreach最多只能为car执行迭代,仅此而已

因此,如果我理解正确,您应该在您的Car构造函数中添加一个list参数,如

    public class Car {
     int a;
     int b;
     public List<string> propNames;

     public Car()
     {
         a = b = 0;
         propNames = new List<string>();
     }

     public Car(int a, int b)
     {
         this.a = a;
         this.b = b;
     }

     public int A {
         set { a = value; }
         get { return a; }
     }

     public int B {
         set { b = value; }
         get { return b; }
     }
}

这叫做反射。您的代码应该如下所示:

foreach (Car car in carColl)
{
    foreach (string propName in propertyNames)
    {
          Console.WriteLine(typeof(Car).GetProperty(propName).GetValue(ent).ToString());
    }
}

在大多数情况下,使用反射是个坏主意,因为它比普通代码更慢,类型安全性也更低。例如,如果尝试访问不存在的属性,则不会出现编译时错误。

car.GetType.GetProperty propName.GetValue car,null可能重复:为什么要这样做?为什么不直接进入汽车?我会很高兴听到你的设计。你正在寻找的是所谓的反射。谢谢!我在尝试戴告诉我的方式。你为什么要发明反射?谢谢!如果我遇到这样的情况,我会参考itOh,现在我知道为什么它不起作用了。谢谢我会记得你说的“坏主意”。谢谢
    public class Car {
     int a;
     int b;
     public List<string> propNames;

     public Car()
     {
         a = b = 0;
         propNames = new List<string>();
     }

     public Car(int a, int b)
     {
         this.a = a;
         this.b = b;
     }

     public int A {
         set { a = value; }
         get { return a; }
     }

     public int B {
         set { b = value; }
         get { return b; }
     }
}
foreach (Car car in carColl)
{    
    foreach (string propName in car.propNames)
    {
        // It is what I want to do. But car.propName don't work
        Console.WriteLine(propName);
    }
}
foreach (Car car in carColl)
{
    foreach (string propName in propertyNames)
    {
          Console.WriteLine(typeof(Car).GetProperty(propName).GetValue(ent).ToString());
    }
}