不使用属性[Inject]和Ninject进行属性注入 [英] Property injection without using attribute [Inject] with Ninject
问题描述
我有一个控制器和一个自定义的ActionInvoker.我可以将自定义的ActionInvoker设置如下,
I have a controller and a customized ActionInvoker. I can set the customized ActionInvoker as following,
public HomeController()
{
this.ActionInvoker = new MyActionInvoker(..);
}
但是不想在每个控制器中都这样做(我也不想使用基本控制器).我想使用Ninject注入ActionInvoker.由于ActionInvoker是基本控制器的一部分,因此无法向其添加[注入].我进行了很多搜索并尝试了
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.
推荐答案
您是否尝试过:
Bind<IActionInvoker>()
.To<MyActionInvoker>()
.Using<SingletonBehavior>();
Bind<HomeController>()
.ToSelf()
.InjectPropertiesWhere(p => p.Name == "ActionInvoker");
这篇关于不使用属性[Inject]和Ninject进行属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!