Generic dependency injection with Unity

后端 未结 2 766
野趣味
野趣味 2021-01-13 22:04

We are wrapping an existing logging library in our own logging service in a C# application to surround it with predefined methods for certain logging situations.

<         


        
2条回答
  •  别那么骄傲
    2021-01-13 22:18

    In your case, when there's simple direct generic-param--to--generic-param mapping the Unity maybe actually handles that, but I doubt that any more advanced cases are not handled, because something at some point of time must provide the mapping of generic-parameters between the types (liek reordering Key-Value vs. Value-Key etc).

    If Dave's answer is not enough, I'm fairly sure that you could write a plugin to Unity/ObjectBuilder that would register a new strategy or set of strategies that would cover just any type mapping you would like, including automatic assembly scanning or materialization of generics.

    See the series of articles at http://www.orbifold.net/default/unity-objectbuilder-part-ii/ and the section near

    Context.Strategies.AddNew< buildkeymappingstrategy >(UnityBuildStage.TypeMapping);
    

提交回复
热议问题