问题
I have a controller and a customized ActionInvoker. I can set the customized ActionInvoker as following,
public HomeController()
{
this.ActionInvoker = new MyActionInvoker(..);
}
But don't want to do this in every controller (and I don't want to use a base controller either). I want to use Ninject to inject the ActionInvoker. Since ActionInvoker is part of the base controller I can't add [inject] to it. I searched a lot and tried something like
Bind<HomeController>().ToSelf().WithPropertyValue("ActionInvoker",
x =>x.Kernel.GetService(typeof(IActionInvoker)));
But it didn't work. Am I doing it wrong? Or any suggestion to achieve what I wanted. Thanks
Update: This works, something else was wrong. Thank you for the answers.
回答1:
Have you tried:
Bind<IActionInvoker>()
.To<MyActionInvoker>()
.Using<SingletonBehavior>();
Bind<HomeController>()
.ToSelf()
.InjectPropertiesWhere(p => p.Name == "ActionInvoker");
来源:https://stackoverflow.com/questions/5896929/property-injection-without-using-attribute-inject-with-ninject