Unity InjectionConstructor for multiparam constructor overriding only single one

前端 未结 3 2090
长发绾君心
长发绾君心 2021-01-03 17:35

I have a class with constructor like this:

public class Bar
{
    public Bar(IFoo foo, IFoo2 foo2, IFoo3 foo3, IFooN fooN, String text)
    {

    }
}
         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 18:13

    Unity can't do this out of the box. The best you can do is:

    container.RegisterType(
        new InjectionConstructor(
            typeof(IFoo), typeof(IFoo2), typeof(IFoo3), typeof(IFooN), "123"));
    

    Or you can use the SmartConstructor provided by the TecX project. This blog post describes some background.

    Registration would look like this:

    container.RegisterType(new SmartConstructor("text", "123"));
    

提交回复
热议问题