C# 4.0 c#泛型错误:类型参数的约束';T';方法。。。?
获取以下错误: 错误1方法的类型参数“C# 4.0 c#泛型错误:类型参数的约束';T';方法。。。?,c#-4.0,generics,entity-framework-4,C# 4.0,Generics,Entity Framework 4,获取以下错误: 错误1方法的类型参数“T”的约束 “genericstuff.Models.MyClass.GetCount(string)”必须与类型的约束匹配 接口方法“genericstuff.IMyClass.GetCount(string)”的参数“T”。考虑 而是使用显式接口实现 类别: public class MyClass : IMyClass { public int GetCount<T>(string filter) where T :
T
”的约束“
genericstuff.Models.MyClass.GetCount(string)
”必须与类型的约束匹配接口方法“
genericstuff.IMyClass.GetCount(string)
”的参数“T
”。考虑而是使用显式接口实现 类别:
public class MyClass : IMyClass
{
public int GetCount<T>(string filter)
where T : class
{
NorthwindEntities db = new NorthwindEntities();
return db.CreateObjectSet<T>().Where(filter).Count();
}
}
公共类MyClass:IMyClass
{
公共int GetCount(字符串过滤器)
T:在哪里上课
{
NorthwindEntities db=新的NorthwindEntities();
返回db.CreateObjectSet().Where(filter.Count();
}
}
接口:
public interface IMyClass
{
int GetCount<T>(string filter);
}
公共接口IMyClass
{
int GetCount(字符串过滤器);
}
在实现中将T泛型参数限制为类。您的界面上没有此约束
您需要将其从类中删除,或将其添加到接口中,以使代码得以编译:
由于您正在调用方法CreateObjectSet()
,因此需要将其添加到接口中
public interface IMyClass
{
int GetCount<T>(string filter) where T : class;
}
公共接口IMyClass
{
int GetCount(字符串过滤器),其中T:class;
}
您也需要将约束应用于接口方法,或者将其从实现中删除
您正在通过更改实现上的约束来更改接口契约-这是不允许的
public interface IMyClass
{
int GetCount<T>(string filter) where T : class;
}
公共接口IMyClass
{
int GetCount(字符串过滤器),其中T:class;
}
您也需要约束接口
public interface IMyClass
{
int GetCount<T>(string filter) where T : class;
}
公共接口IMyClass
{
int GetCount(字符串过滤器),其中T:class;
}
Er lopen hier best wat Nederlanders rond inderdaad!:)