Custom object factory extension for Unity

后端 未结 4 1288
你的背包
你的背包 2021-01-02 06:05

I am using the Unity IoC container, and I need to intercept any calls to Resolve for a certain base interface, and run my own custom code to construct those types.

4条回答
  •  無奈伤痛
    2021-01-02 06:43

    Unity (v2) allows you to specify a factory. It allows a couple of different functions, including taking the type to build up/ name, etc. Simple example:

    UnityContainer cont = new UnityContainer();
    

    have a simple create test method - but this can be expanded with whatever factory you want.
    this will circumvent the normal creation process which is (for the sake of brevity) call the longest constructor all later behaviors including propert setting will still be executed.

    cont.RegisterType(new InjectionFactory(c => CreateTest()));
    
    var svc = cont.Resolve();
    

提交回复
热议问题