dimanche 1 janvier 2017

How to build a generic Type from Type?

Is there any way to build a generic type like this?

Type underlyingType = propertyInfo.PropertyType;
Type genericType = typeof(Action<underlyingType>);

I know it may not be possible. so to get workarounds : I want to create a delegate Action<T> for setter of some properties.

var defaultVal = Activator.CreateInstance(propertyInfo.PropertyType);

var setter = (Action<object>)propertyInfo
    .GetSetMethod(true)
    .CreateDelegate(typeof(Action<object>), this);

var resetter = new Action(() => setter(defval));
return resetter;

This does not work. because I have to pass exact matching type. If Property setter takes double I have to pass Action<double> then it works.

I have Generic container type and Underlying Type in hand. how can I mix them?


Note that I'm working on group of properties taken from a class. not a single property.

var props = GetType().GetProperties(Flags);





Aucun commentaire:

Enregistrer un commentaire