-1

First, thank you for interest and your help !

swiping out the story, the point is following

I have collection with type Car like below

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));

After several process which I said the story, I got a property name 'A' and 'A' is in a List<string> named propertyNames defined as following.

List<string> propertyNames = new List<string>();
propertyNames.Add("A");

Now, I want to do next.

foreach (Car car in carColl)
{
    foreach (string propName in propertyNames)
    {
        // It is what I want to do. But car.propName don't work
        Console.WriteLine(car.propName);
    }
}

Please, let me know how to that... thanks a lot

wallah
  • 1,964
  • 5
  • 27
  • 48
  • 3
    `car.GetType().GetProperty( propName ).GetValue( car, null )` – Dai Nov 25 '16 at 08:51
  • Possible duplicate: http://stackoverflow.com/questions/5508050/how-to-get-a-property-value-based-on-the-name – ErazerBrecht Nov 25 '16 at 08:52
  • 1
    Why would you even want to do that? Why not access `car.A` directly? I will be thrilled to hear about your design. – hyankov Nov 25 '16 at 08:53
  • What you are looking for is called reflection. – xszaboj Nov 25 '16 at 08:54
  • thanks! Im trying the way that **Dai** told. – wallah Nov 25 '16 at 08:55
  • -**Heristo Yankov**, Because the class Car does not exist. What I have to do is 1) Getting any ObservableCollection and 2) Finding names of each Property in the collection and 3) also getting values from the collection using property names which inputted by user thanks for your interest!! – wallah Nov 25 '16 at 08:59

4 Answers4

2

You have to use reflection:

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

In case you're new to reflection: With reflection you access meta information of your object (such as exact type, property names, property types) that would otherwise not be available, because it's dropped during compilation. With that meta information you can access your object, e.g. return property values or execute methods.

Sefe
  • 13,731
  • 5
  • 42
  • 55
2

It's called "reflection". Your code should look like:

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

In most cases using reflection is a bad idea, because it's slower and less type-safe than normal code. For example, there will be no compile-time errors if you will try to access non existant properties.

1

It's because propName is not a property of the class car. You can only access to the properties defined in this Class (car.A, car.B)

0

I think it is pretty obvious you cannot call car.propName because your Car constructor doesn't contain the field named propName (you can call A or B). Also i can never see the relationship between Car and your List.

In case your foreach at most can only perform iteration purpose for car and nothing more.

Thus, if i understood correctly, you properly want to add a list parameter in your Car constructor like

    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; }
     }
}

afterwards, to add "a" in the list, you need to specify which Car you wanted to add "a". For example carColl[0].propName.Add("a");

finally you can print the propName by

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);
    }
}
SKLTFZ
  • 841
  • 2
  • 10
  • 30