Search code examples
c#system.reflection

Check Properties of a Class-Property that is a List of objects


I have a class and some of the class properties are Lists of classes. I wanna now run through all the elements in the List. But whatever I try I can't grab the objects within the list. Does anyone have a workaround for my problem?

 public class car 
 {
    public int id {get; set;}
    public Tire attribute {get; set;}
 }

 public class Tire
 {
    public int id {get; set;}
    public double value {get; set;}
 }

And here my main that creates some classes and the list:

 public class Program
 {
    static void Main(string[] args)
    {
       Tire black = new Tire()
       {
          id = 5
       };
       Tire red = new Tire()
       {
          id = 8
       };
       Tire purple = new Tire()
       {
          id = 10
       };

       //Create my List
       List<Tire> mylist = new List<Tire>();
       List.Add(black);
       List.Add(red);
       List.Add(purple);


       //Define the car
       car mycar = new car()
       {
          id = 20,
          Tire = mylist
       };

I now wanna run through all the elements in my Tire-property-list, but whatever I do I never get the actual object (black, red and purple) within the List-Property. This is what I tried so far:

var type = car.GetType();
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
    if (propertyInfo.ToString().Contains("List"))
    {
        var propValue = propertyInfo.GetValue(this);
        ...

If I Debug and look into the propValue variable I see all my three objects but I can't find a method to actually check them and get their properties again...


Solution

  • There are many problems in your implementation:

    When you populate your list:

    //Create my List
    List<Tire> mylist = new List<Tire>();
    List.Add(black);
    List.Add(red);
    List.Add(purple);
    

    If you want to add your tire in your list, it should be:

    //Create my List
    List<Tire> mylist = new List<Tire>();
    mylist.Add(black);
    mylist.Add(red);
    mylist.Add(purple);
    

    In your class Structure

    Your car has several tires, right? So the class should have a list of tires, not only a tire:

    public class Car 
    {
        public int Id {get; set;}
        public List<Tire> TireList {get; set;}
    }
    

    When you want to instantiate your car

    Your mylist is not accessible inside the constructor call, you must set it up once the constructor has been called (or create a constructor that can have a list of tires in parameter):

    //Define the car
    Car mycar = new Car()
    {
        Id = 20
    };
    
    mycar.TireList = mylist;
    

    QUICK WORKING DEMO

    I did not solved every problem, but here is a working sample (ConsoleApp):

    class Program
    {
        static void Main(string[] args)
        {
            Tire black = new Tire()
            {
                Id = 5
            };
            Tire red = new Tire()
            {
                Id = 8
            };
            Tire purple = new Tire()
            {
                Id = 10
            };
    
            //Create my List
            List<Tire> mylist = new List<Tire>();
            mylist.Add(black);
            mylist.Add(red);
            mylist.Add(purple);
    
    
            //Define the car
            Car mycar = new Car()
            {
                Id = 20
            };
    
            mycar.TireList = mylist;
    
            foreach (var tire in mycar.TireList)
            {
                Console.WriteLine(tire.Id);
            }
    
            Console.ReadKey();
        }
    }
    
    public class Car
    {
        public int Id { get; set; }
        public List<Tire> TireList { get; set; }
    }
    
    public class Tire
    {
        public int Id { get; set; }
        public double Value { get; set; }
    }
    

    Note

    Last but not least, be careful with the use of caps in the name of your objects