2011-05-10 71 views
1

希望我已经正确描述了它。我有一个'通用方法',如下所示。它接受任何Icomparable/Iequatable类型的列表,并返回下面显示的类“compareResult”,其中包含匹配/不匹配项目列表。C#泛型方法拒绝类型,即使它实现了所需的接口

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 
    { 
     ... 

我现在尝试在其中定义见下文“LicencedCustomer”的列表来传递,并实现了的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’不能用作类型参数‘T’泛型类型或方法'MF.Comparers.stepCompare(System.Collections.Generic.List,System.Collections.Generic.List,bool)'。没有从'MFTests.LicencedCustomer'到'System.IComparable'的隐式引用转换。 ..

我以为我已经实现了IComparable,虽然它指的是传递我真的不明白的rsion。对不起,很长的解释,我尽量保持尽可能简短。

有关我在做什么错的任何想法?

回答

5

通用方法不包括通用类型标识符T

where T : IEquatable<T>, IComparable 

应该

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

啊哈!你是对的!非常感谢。 – Glinkot 2011-05-10 03:34:58