Therefore, I am trying to use a combination of MEF and Ninject in the application that I am writing. Basically I add extensions through MEF at runtime. I don't understand how (or if possible) I can update Ninject bindings at runtime.
For example, let's say I have the following element imported by MEF:
[Export(typeof(ICar))] public class BmwCar : ICar { private ICarLogger _carLogger; public BmwCar(ICarLogger carLogger) { _carLogger = carLogger; } public static string Type { get { return "Sedan"; } } public string GetBrand() { return "BMW"; } public static Type InterfaceType { get { return ICar; } } public static Type CarType { get { return GetType(); } } }
Now that I knew this element at compile time, I could just create a Ninject module with the following links, for example:
public class NinjectSetup : NinjectModule { public override void Load() { Bind<CarLogFactory>().ToSelf().InSingletonScope(); Bind<ICarLogger>().ToMethod(x => x.Kernel.Get<CarLogFactory>(new ConstructorArgument("vehicleName", BmwCar.Type)).WhenInjectedInto<BmwCar>(); } }
So the problem is in this line:
Bind<ICarLogger>().ToMethod(x => x.Kernel.Get<CarLogFactory>(new ConstructorArgument("vehicleName", BmwCar.Type)).WhenInjectedInto<BmwCar>();
I'm not sure how to add something like this dynamically after importing BmwCar. Obviously, I cannot use generic at runtime, since a type is needed at compile time. Since I cannot use generics at runtime, it seems something like:
var binding = new BindingBuilder<ICarLogger>(new Binding(typeof(ICarLogger)), this.Kernel).ToMethod(x => x.Kernel.Get<CarLogFactory>(new ConstructorArgument("vehicleName", imported.Type)).WhenInjectedInto<imported.CarType>();
not an option. Does anyone know what to do to create new bindings at runtime?