vendredi 16 octobre 2020

Getting the count property value of a list property inside a class with reflection

I have the following classes:

class Topping
{
   public string Name {get; set;}
}

class Pizza
{
    public List<Topping> Toppings {get ; set;}

    public Pizza()
    {
       this.Toppings = new List<Topping>();
    }
}

Suppose i have a list of pizzas inside Main

Is there a way to get the value of the Count property of the Toppings list inside the Pizza class, using reflection ?

I tried something like this:

foreach(var pizza in Pizza)
{
   int countValue = pizza.GetType().GetProperty("Toppings").GetType().GetProperty("Count").GetValue(pizza);
}




Aucun commentaire:

Enregistrer un commentaire