mardi 8 septembre 2015

Reflection : Get the real type of a initialized auto-property (C#6)

I have a class declared like this :

public class MyClass
{
    public IMyInterface1 Prop1 { get; } = new MyImplementation1();
    public IMyInterface2 Prop2 { get; } = new MyImplementation2();
    public IMyInterface3 Prop3 { get; } = new MyImplementation3();
    //[...]
}

I would like the list of implemented types, using reflection. I dont have an instance of MyClass, just the type.

Ex:

static void Main(string[] args)
{
    var aList = typeof(MyClass).GetProperties(); // [IMyInterface1, IMyInterface2, IMyInterface3]
    var whatIWant = GetImplementedProperties(typeof(MyClass)); // [MyImplementation1, MyImplementation2, MyImplementation3]
}

IEnumerable<Type> GetImplementedProperties(Type type)
{
    // How can I do that ?
}

PS: I'm not sure the title is well adapted, but I have found nothing better. I am open to suggestions.





Aucun commentaire:

Enregistrer un commentaire