小编Ант*_*дин的帖子

如何从嵌套类通过依赖注入服务进行访问?

假设我有这段代码(一般而言),假设我需要在 C 类中使用 ILogger,而在 A 类和 B 类中不需要它

public class A
{
    var classB = new B();
}
public class B
{
    var classC = new C();
}
public class C
{
    //Here I want to use the Ilogger service
}
Run Code Online (Sandbox Code Playgroud)

但沿着这整条链条传递构造函数似乎不合理,而且随着使用的服务的增加,构造函数只会增长。那么如何正确调用C类的服务呢?谢谢

c# dependency-injection asp.net-core

1
推荐指数
1
解决办法
530
查看次数

标签 统计

asp.net-core ×1

c# ×1

dependency-injection ×1