Ene*_*Boy 0 c# dependency-injection interface idictionary .net-6.0
我想将 a 注入IDictionary到{key, interface}构造函数中,但我不知道如何在构造函数中设置它program.cs
我可以启动接口,也可以启动接口IDictionary,但我不知道如何组合它们。
任何建议都会被采纳。
附加背景:
所以我需要注入我的服务,例如
我需要注入服务,例如,
s.AddTransient<IFooService, AFooService>();
s.AddTransient<IFooService, BFooService>();
Run Code Online (Sandbox Code Playgroud)
但在我想要的构造函数中
public MyClass(IDictionary<string, IFooService> fooServices)
Run Code Online (Sandbox Code Playgroud)
services.AddTransient<MyClass>();
services.AddTransient<AFooService>();
services.AddTransient<BFooService>();
services.AddTransient<IDictionary<string, IFooService>>(sp =>
new Dictionary<string, IFooService>
{
{ "A", sp.GetRequiredService<AFooService>() },
{ "B", sp.GetRequiredService<BFooService>() },
});
``
Run Code Online (Sandbox Code Playgroud)