问题
I have a small issue with my simple example.
I have simple factory interface:
public interface ICameraFactory
{
ICameraController GetNikonCamera();
ICameraController GetCanonCamera();
}
I bind it as a factory:
IKernel kernel = new StandardKernel();
kernel.Bind<ICameraFactory>().ToFactory();
When i try to convert:
kernel.Bind<ICameraController>().To<NikonCameraController>()
.Named("NikonCamera");
to:
kernel.Bind<ICameraController>().To<NikonCameraController>()
.NamedLikeFactoryMethod<ICameraFactory>(f => f.GetNikonCamera());
it's don't compile.
For example, this code is compiled (but it's terrible):
kernel.Bind<ICameraController>()
.ToMethod<ICameraController>(c=>new NikonCameraController())
.NamedLikeFactoryMethod<ICameraController, ICameraFactory>(f => f.GetNikonCamera());
What am I doing wrong?
Ninject 3.0.1.10
Ninject.Extension.Factory 3.0.1.0
Compile error: https://dl.dropbox.com/u/21806986/Screenshots/shot_19072012_133454.png
回答1:
You can use:
this.kernel.Bind<ICameraController>()
.To<NikonCameraController>()
.NamedLikeFactoryMethod((ICameraFactory f) => f.GetNikonCamera());
来源:https://stackoverflow.com/questions/11557856/namedlikefactorymethod-in-ninject-extensions-factory-working-in-non-compliance-w