Ninject Custom Inject Attribute
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.
It's already supported and can be done with a simple configuration of the kernel.
new StandardKernel(new NinjectSettings() { InjectAttribute = typeof(MyOwnInjectAttribute) };
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.
精彩评论