我有一个抽象类示例。另一个泛型类 UsesExample 使用它作为一个约束,带有一个 new() 约束。后来,我为 Example 类创建了一个子类 ExampleChild,并将它与泛型类一起使用。但不知何故,当泛型类中的代码尝试创建新副本时,它调用的不是子类中的构造函数,而是父类中的构造函数。为什么会这样?这是代码:
abstract class Example {
    public Example() {
        throw new NotImplementedException ("You must implement it in the subclass!");
    }
}
class ExampleChild : Example {
    public ExampleChild() {
        // here's the code that I want to be invoken
    }
}
class UsesExample<T> where T : Example, new() {
    public doStuff() {
        new T();
    }
}
class MainClass {
    public static void Main(string[] args) {
        UsesExample<ExampleChild> worker = new UsesExample<ExampleChild>();
        worker.doStuff();
    }
}