开发者

Ninject Custom Inject Attribute

开发者 https://www.devze.com 2023-01-25 09:05 出处:网络
Could someone point me in the direction of how to create a custom \"Inject\" attribute with Ninject? I would like to do the following:

Could someone point me in the direction of how to create a custom "Inject" attribute with Ninject?

I would like to do the following:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号