出现以下错误:
Error 1 The constraints for type parameter '
T
' of method
'genericstuff.Models.MyClass.GetCount<T>(string)
' must match the constraints for type
parameter 'T
' of interface method 'genericstuff.IMyClass.GetCount<T>(string)
'. Consider
using an explicit interface implementation instead.
类(class):
public class MyClass : IMyClass
{
public int GetCount<T>(string filter)
where T : class
{
NorthwindEntities db = new NorthwindEntities();
return db.CreateObjectSet<T>().Where(filter).Count();
}
}
界面:
public interface IMyClass
{
int GetCount<T>(string filter);
}
最佳答案
您将 T 泛型参数限制为实现中的类。您的界面没有此限制。
您需要将其从类中删除或将其添加到接口(interface)中才能编译代码:
由于您正在调用方法CreateObjectSet<T>()
,其中 requires the class constraint ,您需要将其添加到您的界面中。
public interface IMyClass
{
int GetCount<T>(string filter) where T : class;
}
关于c#-4.0 - C# 泛型错误 : The constraints for type parameter 'T' of method . ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11845964/