vendredi 20 octobre 2017

C# Ninject binding from Dictionary

I'm using Ninject lib in my project. I have a task: I need to bind interfaces to services by passed Dictionary, i prefer to use reflection.

Without reflection this is done so:

kernel.Bind<IUser>().To<User>();

Where IUser - interface, User - IUser implementation.

In reflection i doing so:

MethodInfo method = kernel.GetType().GetMethods().FirstOrDefault(x=>x.Name == "Bind");
MethodInfo genericBind = method.MakeGenericMethod(bind.Key);
MethodInfo bindResult = genericBind.Invoke(kernel,null).GetType().GetMethods().FirstOrDefault(x => x.Name == "To" && x.IsGenericMethod == true);
MethodInfo genericTo = bindResult.MakeGenericMethod(bind.Value);
genericTo.Invoke(kernel, null); //Error is here

but I get an error System.Reflection.TargetException.

What is wrong?

Sorry for my English :-)




Aucun commentaire:

Enregistrer un commentaire