2013-01-15 44 views
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() 
{ 
} 

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

回答

2

你需要把限制上地方:

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

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