dimanche 24 octobre 2021

How to get properties of a class which are IEnumerable of a class or an IEnumerable of a class inherited from that class [duplicate]

In c# I have a class with properties on it which returns IEnumerables.

public class MyClass
{
   public IEnumerable<MyPropClass1> Prop1 { get { return _prop1; } }
   public IEnumerable<MyPropClass2> Prop2 { get { return _prop2; } }
   public IEnumerable<AnotherClassAltogether> Prop3 {get {return _prop3; } }
   ...
}

Lets say MyPropClass1 and MyPropClass2 both inherit from MyPropBaseClass but AnotherClassAltogether doesn't.

What is the simplest way to get all properties from MyClass which either are an IEnumerable of a certain class, or an IEnumerable of a class which is somewhere down the chain is inherited from that class?

E.g. if I wanted to query MyClass for properties based on IEnumerables templated to something that is based on MyPropBaseClass then it should return Prop1 and Prop2

For more clarity (hopefully to address the close vote) a pseudo function to answer this question would be something like:

var properties = GetIEnumerablePropertiesOfType(typeof(MyClass), typeof(MyPropBaseClass))

and this would return a list of properties containing Prop1 and Prop2 (as an enumerable of System.Reflection.PropertyInfo)





Aucun commentaire:

Enregistrer un commentaire