mercredi 27 mai 2015

How can I resolve the Class type for generic parameter, when the parameter value is being passed as an interface?

Consider a method with the following signature:

void foo(List<T> myList) ...

Let's assume, by using reflection, you need to construct such a function and obtain the PropertyInfo details of the T type parameter. Calling typeof(T).GetProperties(...) should do the trick, so we can add the following line to our method, to obtain these details.

void foo(List<T> myList)
{
    PropertyInfo[] props = 
        typeof(T).GetProperties(BindingFlags.Public 
            | BindingFlags.Instance 
            | BindingFlags.FlattenHierarchy);

    ...
}

This provides the information we need... except when T is an interface parameter, what I'm finding is that props only contains the interface properties and not the properties associated with class that is within the list, which inherits from the interface.

To be clear, my interface AND my class definitions are public, as are the properties within my classes.

How can I get the properties associated with the actual type that inherits an interface rather than strictly the interface properties?





Aucun commentaire:

Enregistrer un commentaire