Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 c#泛型错误:类型参数的约束';T';方法。。。?_C# 4.0_Generics_Entity Framework 4 - Fatal编程技术网

C# 4.0 c#泛型错误:类型参数的约束';T';方法。。。?

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 :

获取以下错误:

错误1方法的类型参数“
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!:)