0

我有以下代码

public interface IDummy<TType> where TType : new()
{
}

public class Dummy<TType> : IDummy<TType>
{
}

并且编译失败,因为Error 1 'TType' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TType' in the generic type or method 'ConsoleApplication1.IDummy<TType>' D:\Temp\ConsoleApplication1\Dummy.cs 5 18 ConsoleApplication1

但是如果我将代码更改为

public interface IDummy<TType>
{
}

public class Dummy<TType> : IDummy<TType> where TType : new()
{
}

然后编译成功。我无法在接口级别定义泛型类型要求?

4

1 回答 1

3

您需要对这两个地方进行限制:

public interface IDummy<TType> where TType : new()
{
}

public class Dummy<TType> : IDummy<TType> where TType : new()
{
}
于 2013-01-15T20:47:52.437 回答