I have an object model like this:
public class MyClass
{
int TheProp {get;set;}
List<Object1> TheListOfObject1 {get;set;}
List<Object2> TheListOfObject2 {get;set;}
List<Object3> TheListOfObject3 {get;set;}
List<Object4> TheListOfObject4 {get;set;}
}
Now some the the nested objects contain a property called MyField and some of the lists might have objects in them and some don’t.
I’m looking to loop through every list and if the objects in the list contain the property MyField then set that property to 1.
What’s the best way to do this? I could hard-code the loop and only loop through the lists for which I know there’s the property I’m looking for and then test if the list has a count > 0 but if I then add another list of nested object I’d have to rewrite that part too. What’s a more general way to do it?
Thanks.
Either use reflection or write an interface containing that property
and implement it in all your classes which have that property. Then you can use