我想知道为什么不显式使用 IServiceProvider 来解决依赖项而不是单独注入每个依赖项。换句话说,为什么要使用这种方法:
public class A
{
private B _b;
private C _c;
private D _d;
private E _e;
public A(B b, C c, D d, E e)
{
_b = b;
_c = c;
_d = d;
_e = e;
}
}
Run Code Online (Sandbox Code Playgroud)
而不是这个:
public class A
{
private B _b;
private C _c;
private D _d;
private E _e;
public A(IServiceProvider sp)
{
_b = (b) sp.GetService(typeof(b));
_c = (c) sp.GetService(typeof(c));
_d = (d) sp.GetService(typeof(d));
_e = (e) sp.GetService(typeof(e)); …Run Code Online (Sandbox Code Playgroud)