我有一个基类Class Base具有dependecy Dep和default和Injection Constructor-
Class Base : IBase
{
public IDep Dep { get; set; }
public Base()
{
Console.WriteLine("Default Constructor Base ");
}
[InjectionConstructor]
public Base(IDep dep)
{
Console.WriteLine("Injection Constructor Base ");
Dep = dep;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为在解析派生类时,应该自动注入Dependency dep(通过Constructor Injection).
但是,当我从它派生一个类并解析该类时,这似乎不起作用,而是调用Base的默认构造函数.
当我从Derived Class中显式调用构造函数时,我只能使它工作.
class Derived : Base
{
public Derived ()
{
Console.WriteLine("Default Constructor Derived ");
}
public Derived (IDep dep) : base(dep1)
{
Console.WriteLine("Injection Constructor Derived ");
}
}
Run Code Online (Sandbox Code Playgroud)
unity是否提供了任何直接的方法来隐式调用基类的注入构造函数(而不是通过显式的Construtor调用)?如果没有,是否有任何理由为什么统一容器本身不做?
c# dependency-injection unity-container constructor-injection