dimanche 18 décembre 2016

Is it possible to obtain the type of a property of an unknown type?

public class TypeA {}

public class TypeB
{
    TypeA PropertyA { get ;set; }
}

There are two classes of types TypeA and TypeB in an assembly that we are unaware of at compile-time. The only thing known is the name of the property PropertyA.

I would like to obtain the type of this property at run-time, if it is possible.

T GetGenericObject<T>()
{
    T result = (T)Activator.CreateInstance(typeof(T));
    PropertyInfo pInfo = typeof(T).GetProperty("PropertyA");

}

Assuming this method is called with TypeB,

var obj = GetGenericObject<TypeB>();

I can access the PropertyInfo of PropertyA but its type seems to be undeterminable.





Aucun commentaire:

Enregistrer un commentaire