2011-05-10 74 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