Home > front end >  How do I recursively read properties of an Object
How do I recursively read properties of an Object

Time:03-19

I have the following Class. More properties can be added at any time in its life

public class Foo
{
    public string propertyA { get; set; }
    public string propertyB { get; set; }
    public string propertyC { get; set; }
}

I have an instance of "Foo" and it's populated with the following values

   var myFoo = new Foo()
        {
            propertyA = "X valueA",
            propertyB = "valueB",
            propertyC = "X valueC",
        };

How do i go about recursively getting the count of the properties (in this example) with the value "X" in its string value?

CodePudding user response:

What is the Expected Value of X?

you can loop it with a function like that:

foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
{
   // do stuff here
}
  • Related