我正在使用Microsoft Unity.我有一个接口ICustomerService及其实现CustomerService.我可以使用以下代码为Unity容器注册它们:
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager());
Run Code Online (Sandbox Code Playgroud)
如果CustomerService在其构造函数中有某个参数(例如ISomeService1),我使用以下代码(我需要指定SomeService1):
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1()));
Run Code Online (Sandbox Code Playgroud)
这里没问题.
当CustomerService类在其构造函数中有两个参数(不是前一个示例中的一个参数)时会出现问题(例如ISomeService1和ISomeService2).当我使用以下代码时它工作正常:
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1(), new SomeService2()));
问题是我不想指定SomeService2()第二个参数.我只想指定第一个参数 - SomeService1().但我得到的错误是我需要指定一个或两个参数.
如何只指定构造函数的第一个参数?