lundi 28 mars 2016

Handling generic properties via reflection

If I have the following wrapper class:

public class Wrapper<T>
{
    public T Data { get; set; }
    public string[] Metadata { get;set;
}

and another class then exposes that value without generics:

public class SomeOtherClass
{
    public object WrappedData { get;set };
}

, how can I get at the original unwrapped data?

I can test for it, using something like:

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Wrapper<>))
{
    dynamic originalValue = someOtherClass.WrappedData;
}

but I can't then call the .Data property on originalValue, getting a RuntimeBinderException.





Aucun commentaire:

Enregistrer un commentaire