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#表达式的逆变 公共接口IMasterService:IDisposable { T GetByID(int-id); IEnumerable GetAll(); IEnumerable,其中(表达式func); int AddNew(T newEntity); }_C# 4.0 - Fatal编程技术网

C# 4.0 C#表达式的逆变 公共接口IMasterService:IDisposable { T GetByID(int-id); IEnumerable GetAll(); IEnumerable,其中(表达式func); int AddNew(T newEntity); }

C# 4.0 C#表达式的逆变 公共接口IMasterService:IDisposable { T GetByID(int-id); IEnumerable GetAll(); IEnumerable,其中(表达式func); int AddNew(T newEntity); },c#-4.0,C# 4.0,无效差异:类型参数“T”在“IMasterService.Where(System.Linq.Expressions.Expression>)”上必须相反有效T'是协变的。简而言之:如果要使用,则应返回T,如果要使用则需要将T作为参数传递。不能在一个界面上同时使用out和in。现在我不能确切地说为什么你不能使用表达式,但我会尝试找出其中的一些东西。删除中的out可以避免错误,但我想(希望)你放它不是为了好玩?我肯定需要用泛型实现我的协方差。它与GetByID和GetAll方法配合得很好。但是,我

无效差异:类型参数“T”在“IMasterService.Where(System.Linq.Expressions.Expression>)”上必须相反有效T'是协变的。

简而言之:如果要使用
,则应返回
T
,如果要使用
则需要将
T
作为参数传递。不能在一个界面上同时使用
out
in
。现在我不能确切地说为什么你不能使用
表达式,但我会尝试找出其中的一些东西。

删除
中的out可以避免错误,但我想(希望)你放它不是为了好玩?我肯定需要用泛型实现我的协方差。它与GetByID和GetAll方法配合得很好。但是,我尝试使用where(Expression>时,它会给我这个错误。请在下面找到完整的代码,看看我试图实现什么。基本上与泛型一致。它适用于前两种方法(GetByID和GetAll)。但它在何处失败。它给出了一个错误无效的方差:类型参数“T”必须在“IMasterService.Where(System.Linq.Expressions.Expression>)”上反向有效。“T”是协变的。请在此处查找代码
public interface IMasterService<out T> : IDisposable
{
        T GetByID(int id);
        IEnumerable<T> GetAll();
        IEnumerable<T> Where(Expression<Func<T, bool>> func);
        int AddNew(T newEntity);
}