C# 泛型方法拒绝类型,即使它实现了所需的接口
希望我的描述是正确的。我有一个“通用方法”,如下所示。它接受任何 Icomparable/Iequatable 类型的列表,并返回如下所示的类“compareResult”,其中包含匹配/不匹配项的列表。
public partial class Comparers
{
public class compareResult<T>
{
public List<T> unchangedItems;
public List<T> changedItems;
public List<T> leftOrphans;
public List<T> rightOrphans;
}
public static compareResult<T> stepCompare<T>(List<T> leftList, List<T> rightList, bool confirmUniqueIDs = true) where T : IEquatable<T>, IComparable
{
...
我现在尝试传入定义如下的“LicensedCustomer”列表,并实现 CompareTo 和 Equals 方法来实现 IComparable/IEquatable 接口。
public class LicencedCustomer : IEquatable<LicencedCustomer>, IComparable<LicencedCustomer>
{
public string LMAA_CODE {get; set;}
...
现在我尝试传递以下两个客户列表:
Comparers.compareResult<LicencedCustomer> result = new Comparers.compareResult<LicencedCustomer>();
result = Comparers.stepCompare(leftList, rightList);
但它显示“错误 1 类型 'MFTests.LicencedCustomer' 不能用作泛型类型或方法 'MF.Comparers.stepCompare(System.Collections) 中的类型参数 'T' .Generic.List, System.Collections.Generic.List, bool)' 没有从 'MFTests.LicencedCustomer' 到的隐式引用转换。 'System.IComparable'...
我以为我已经实现了 IComparable,尽管它指的是我不太理解的转换。抱歉,我的解释很长,我试图尽可能简短地表达
我的想法 。做错了吗?
Hopefully I've described it correctly. I have a 'generic method' which looks like the below. It accepts a list of any Icomparable/Iequatable type and returns a class 'compareResult' shown below containing lists of matched/unmatched items.
public partial class Comparers
{
public class compareResult<T>
{
public List<T> unchangedItems;
public List<T> changedItems;
public List<T> leftOrphans;
public List<T> rightOrphans;
}
public static compareResult<T> stepCompare<T>(List<T> leftList, List<T> rightList, bool confirmUniqueIDs = true) where T : IEquatable<T>, IComparable
{
...
I now try to pass in a list of 'LicencedCustomer' which is defined as below, and implements the CompareTo and Equals methods to implement the IComparable/IEquatable interfaces.
public class LicencedCustomer : IEquatable<LicencedCustomer>, IComparable<LicencedCustomer>
{
public string LMAA_CODE {get; set;}
...
Now I try to pass two lists of customers per below:
Comparers.compareResult<LicencedCustomer> result = new Comparers.compareResult<LicencedCustomer>();
result = Comparers.stepCompare(leftList, rightList);
But it says "Error 1 The type 'MFTests.LicencedCustomer' cannot be used as type parameter 'T' in the generic type or method 'MF.Comparers.stepCompare(System.Collections.Generic.List, System.Collections.Generic.List, bool)'. There is no implicit reference conversion from 'MFTests.LicencedCustomer' to 'System.IComparable'...
I thought I had implemented IComparable though it refers to conversion which I don't really understand. Sorry for the long explanation, I tried to keep it as brief as possible.
Any thoughts on what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
泛型方法不包含泛型类型标识符
T
。应该是
The generic method does not include the generic type identifier,
T
.should be