简体   繁体   English

new()和抽象基类的泛型类型约束

[英]Generic type constraint of new() and an abstract base class

Here we have a simple class herarchy, and use of generics with a type constraint of new() 这里我们有一个简单的类层次结构,并使用带有new() 类型约束的泛型

public abstract class Base 
{
}

public class Derived : Base
{
}

public class TestClass
{
    private void DoSomething<T>(T arg) where T : new()
    {
    }

    public void TestMethod()
    {
        Derived d1 = new Derived();
        DoSomething(d1); // compiles

        Base d2 = new Derived();
        DoSomething(d2); // compile error
    }
}

The code fails to compile at the indicated line, with an error of: 代码无法在指定的行编译,错误为:

'Base' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Foo.DoSomething(T)' 'Base'必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法'Foo.DoSomething(T)'中将其用作参数'T'

This error is clear and makes sense, but I had hoped that the compiler would understand that all derivations of Base (that could be instantiated at this point) do have a public parameterless constructor. 这个错误是明确的,也是有道理的,但我曾希望编译器能够理解Base所有派生(可以在这一点实例化)都有一个公共无参数构造函数。

Would this be theoretically possible for the compiler? 理论上这对编译器来说是否可行?

Alas you have to explicitly give the type 唉,你必须明确给出类型

DoSomething<Derived>(d2);

theoretically is not possible to create something that is abstract 从理论上讲,不可能创造出抽象的东西

new Constraint (C# Reference): 新约束(C#参考):

To use the new constraint, the type cannot be abstract. 要使用新约束,类型不能是抽象的。

Calling: 呼叫:

Base d2 = new Derived();
DoSomething(d2);

You are in fact doing: 你其实在做:

Base d2 = new Derived();
DoSomething<Base>(d2);

Since the Base is abstract, compilation error occurs. 由于Base是抽象的,因此会发生编译错误。

So, you have to cast explicitly: 所以,你必须明确地投射:

Base d2 = new Derived();
DoSomething((Derived) d2);

How could you ensure the compiler, that anyone ever put there something, that is not abstract? 你怎么能确保编译器,任何人都放在那里,不是抽象的?

The only manner that I see would be, if we got a keyword like "must-inherit-to-non-astract" and then create public must-inherit-to-non-abstract abstract class Base . 我看到的唯一方式是,如果我们得到一个关键字,例如“must-inherit-to-non-astract”,然后创建public must-inherit-to-non-abstract abstract class Base After that, the compiler could be sure, that if you put the base instance into your method, that will be in fact a subclass, that is non-abstract and therefore can be instantiated. 在那之后,编译器可以肯定,如果你将基本实例放入你的方法中,那实际上是一个子类,它是非抽象的,因此可以实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 泛型类型约束,其中类实现抽象类 - Generic Type Constraint where class implements an abstract class 抽象类型和交互作用的问题作为泛型类型params与new()约束 - Problems with abstract types and interafaces as generic type params with the new() constraint 泛型类型,其类型参数是抽象基类 - Generic type whose type parameter is an abstract base class C#。 是否可以使用具有基类型约束的静态泛型类,该基类型约束具有带有进一步基类型约束的方法 - C#. Is is possible to have a static generic class with a base type constraint that has a method with a further base type constraint 转换为抽象的通用基类 - casting to an abstract generic base class 将方法上的泛型类型约束为从抽象泛型基类的任何变体形式派生的任何类 - Constrain generic type on a method to be any class that is derived from any variant form of an abstract generic base class 使用抽象 class 作为泛型类型 - Using abstract class as a generic type 具有新类型约束的泛型构造函数 - Generic constructor with new type constraint 如何在另一个通用基类上添加C#泛型类型约束? - How to add a C# generic type constraint on another generic base class? 忽略通用抽象基类中的NUnit测试 - NUnit Tests in generic abstract base class are ignored
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM