Ninject 自定义注入属性
有人可以指导我如何使用 Ninject 创建自定义“注入”属性吗?
我想执行以下操作:
- InjectView - 自定义“注入”属性
当要注入具有此属性的属性时,将从“ViewManager”类加载注入的值。
我找到了一个使用 ISelector 的示例来决定是否要注入属性/字段,但是我无法找到如何为此添加自定义“注入策略” - 我想将实际值的注入委托给我的视图管理器。
Could someone point me in the direction of how to create a custom "Inject" attribute with Ninject?
I would like to do the following:
- InjectView - custom "inject" attribute
When a property with this attribute is to be injected, the injected value is to be loaded from a "ViewManager" class.
I found an example with ISelector to decide if the property / filed is to be injected, however I couldn't find out how to add a custom "injection strategy" for this - I would like to delegate the injection of the actual value to my ViewManager.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它已经受到支持,并且可以通过简单的内核配置来完成。
It's already supported and can be done with a simple configuration of the kernel.
我通过添加自定义 IInjectionHeuristic 解决了这个问题,它允许通过我的自定义属性进行注入。然后我还添加了一个自定义 IBindingResolver,它为每个已解析的类型添加了一个附加绑定 - 该绑定具有对自定义属性的条件检查,因此它不会破坏任何以前的绑定...
由自定义 IBindingResolver 集创建的绑定本地“ProviderCallback”,它利用提取的属性并将请求传递到内部 ViewRegionManager 实例。
希望这有助于将来有人想做类似的事情。
I solved this by adding a custom IInjectionHeuristic which allows injecting by my custom attribute. Then I also added a custom IBindingResolver which adds an additional binding per type that is resolved - this binding has a condition checking for the custom attribute, so that it doesn't break any previous bindings...
Tha bindings created by the custom IBindingResolver sets a local "ProviderCallback", which utilizes the extracted property and passes the request to an internal ViewRegionManager instance.
Hope this helps in case anyone wants to do something similar in the future.