vendredi 16 octobre 2015

Call Chained Generic Methods Dynamically

I'm using part of the FubuCore project to get a Strong Typed Config. I've only been able to get the Settings Classes to work with concrete types, if I want to inject a "Settings Interface" into a class I have to do this

For<IDbSettings>().Use(x => x.GetInstance<DefaultDbSettings>());

Is it possible to do this dynamically?

So the psuedo code would be

foreach (var setting in settingsDict)
{
   For<setting["key"]>().Use(x => x.GetInstance<setting["value"]>());
}

So far I've only been able to invoke the For method and haven't been able to get them to chain.

var forMethod = typeof(ConfigRegistry).GetMethod("For", new Type[] {typeof(ILifecycle)});

foreach (var @interface in interfaces)
{
    MethodInfo genericMethod = forMethod.MakeGenericMethod(@interface);

    var closedGenericForMethod = genericMethod.Invoke(this, new[] { Lifecycles.Singleton });
}

I'm thinking this isn't going to be possible?





Aucun commentaire:

Enregistrer un commentaire