【问题标题】:Dependency Injection From class Library来自类库的依赖注入
【发布时间】:2019-01-13 17:46:53
【问题描述】:

我正在使用 .Net core 2.0 并实现了构造函数依赖注入 (DI)。使用控制器构造函数,一切都按预期工作。

例如,如果在类库中我想创建一个对象,我如何在不使用构造函数的情况下调用它。

public interface IAppConfig
{
     string Test();

}

public class AppConfig : IAppConfig
{
    public AppConfig(IConfiguration configuration)
    {

    }

    public string Test()
    {
        return "Done";
    }
}

【问题讨论】:

    标签: c# dependency-injection .net-core .net-core-2.0


    【解决方案1】:

    你不能。即使你使用 IoC 容器,它也会在依赖树解析后调用构造函数。任何对象都是通过显式或隐式调用构造函数来创建的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      相关资源
      最近更新 更多