mercredi 24 janvier 2018

How do I create a delegate for a property getter from an open generic type

I have a PropertyInfo for a property on an open generic type, obtained in the following way:

public interface ITest<T>
{
    T Item { get; }
}

var propertyInfo = typeof(ITest<>).GetProperty("Item");

I'd like to be able to create a concrete, callable delegate, filling in the class type parameter (e.g. Func<ITest<int>, int>) from propertyInfo.

The obvious thing to try is propertyInfo.GetMethod.MakeGenericMethod(typeof(int)).CreateDelegate(...), but this fails because GetMethod isn't generic - it's a property on a generic class.

Is there a way to create this delegate, or can it only come about by using typeof(ITest<int>) to start with?





Aucun commentaire:

Enregistrer un commentaire