mardi 28 avril 2015

IndexOutOfRangeException when trying to access the generic arguments of a Func

I need to get the generic argument type from a factory delegate (Func<object), but it always results in a IndexOutOfRangeException.

public void Bind(Type service, Func<object> factory) {
    var factoryResultType = factory.Method.GetGenericArguments()[0]; // ex
}

// extension
public static void Bind<TService>(this IBinder binder, Func<TService> factory)
    where TService : class 
{
    binder.Bind(typeof(TService), (Func<object>) factory);
}

// usage
var dummyClass = new DummyClass();
binder.Bind<IDummy>(() => dummyClass);





Aucun commentaire:

Enregistrer un commentaire