C# 泛型方法拒绝类型,即使它实现了所需的接口

发布于 2024-11-06 06:06:56 字数 1425 浏览 0 评论 0原文

希望我的描述是正确的。我有一个“通用方法”,如下所示。它接受任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-11-13 06:06:56

泛型方法不包含泛型类型标识符 T

where T : IEquatable<T>, IComparable

应该是

where T : IEquatable<T>, IComparable<T>

The generic method does not include the generic type identifier, T.

where T : IEquatable<T>, IComparable

should be

where T : IEquatable<T>, IComparable<T>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文