2012-11-13 28 views
5

誰能給我解釋一下什麼是錯在下面的類聲明。該錯誤是以下內容:C#泛型類和EqualityComparer

錯誤CS0176:靜態成員`的Object.Equals(對象,對象)」不能 與實例引用訪問,具有類型名 代替

限定它我無法看到我在使用實例引用的位置。


對不起,我的錯。我發了一個不完整的問題。 只是爲了保持完整性,Idetifier類只是以下幾點:使用EqualityComparer出現,是由於複製和粘貼錯誤(對不起你們,太多的通用代碼今天)

public interface Identifier<ID_TYPE> 
{ 
    ID_TYPE Id{get;set;} 
} 

當然我的問題是錯誤的,因爲我沒有給你所有你需要回答的元素(我會盡快刪除它)。我需要ITypeIConvertible。 感謝所有反正。

回答

1

最後我解決了這個辦法:

private class PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> : 
     IComparer<PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType>> 
      where TPriorityValue : IComparable 
      where IIdentifiableEntry : Identifier<IType> 
      where IType : IConvertible 
    { 
     public TPriorityValue Priority{get;private set;} 
     public IIdentifiableEntry Entry{get;private set;} 

     public PriorityQueueEntry(TPriorityValue val,IIdentifiableEntry entry) 
     { 
      Priority = val; 
      Entry = entry; 
     } 
     public int Compare(PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> first, PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> second) 
     { 
      if (first.Priority.CompareTo(second.Priority) < 0) 
      { 
       return -1; 
      } 
      else if (first.Priority.CompareTo(second.Priority) > 0) 
      { 
       return 1; 
      } 
      return first.Entry.Id.ToUInt32(NumberFormatInfo.CurrentInfo) < first.Entry.Id.ToUInt32(NumberFormatInfo.CurrentInfo) ? -1:1; 
     } 
    } 
6

這是一個實例參考:

EqualityComparer<IIdentifiableEntry>.Default 

第一個問題是,你不希望打電話object.Equals(object, object)可言。你真的想在相等比較器上調用方法 - 但你試圖用不可轉換爲IIdentifieableEntry的參數調用它。

的第二個問題是,你要執行排序比較,而不是一個平等比較,所以你要Comparer<T>,不EqualityComparer<T>

目前尚不清楚你想要達到什麼樣的,但是這個代碼將至少編譯

return Comparer<IIdentifiableEntry>.Default.Compare(first.Entry, second.Entry); 

如果你真的要比較的Id屬性,需要一個相等比較爲ID屬性類型 - 我們不知道這是什麼類型。

我懷疑它更可能是你真正想要的東西這樣的:

return Comparer<string>.Default.Compare(first.Entry.Id, second.Entry.Id); 

...但它取決於Id類型。

+6

此代碼不能編譯,因爲Equals返回一個布爾和方法應該返回一個int –

+0

@WouterdeKort:伊克。謝謝,會編輯。 –

+0

其實IType:IConvertible解決了我的問題。對不完整的問題抱歉。 – Heisenbug

3

您還沒有顯示標識符和EqualityComparer的聲明。但我認爲你需要行更改爲類似:

return EqualityComparer<IIdentifiableEntry>.Default.Equals<IType>(first.Entry.Id, second.Entry.Id); 

[編輯]

按Jon的評論。你根本不想要相等比較器。 Asumming這是Entry.Id是IComparable的,然後只是:

return first.Entry.Id.CompareTo(second.Entry.Id); 

我會建議進入僅限於IComparable的,所以我們得到的東西,如:

類PriorityQueueEntry> 其中TPriorityValue:IComparable的 其中TEntry :IComparable的 { 公共TPriorityValue優先{獲取;私人集;} 公共TEntry項{獲取;私人集;}

public PriorityQueueEntry(TPriorityValue val, TIdentifiableEntry entry) 
    { 
     Priority = val; 
     Entry = entry; 
    } 
    public int Compare(PriorityQueueEntry<TPriorityValue, TEntry first, PriorityQueueEntry<TPriorityValue, TEntry> second) 
    { 
     if (first.Priority.CompareTo(second.Priority) < 0) 
     { 
      return -1; 
     } 
     else if (first.Priority.CompareTo(second.Priority) > 0) 
     { 
      return 1; 
     } 
     return first.Enrtry.CompareTo(second.Entry); 
    } 
} 

如果TEntry是一個類,您可能需要添加一些空檢查。

+1

'EqualityComparer '是一個框架類。 –

+0

'Equals'在'EqualityComarer ' – 2kay

+0

中沒有模板化方法我的不好。我google了它,它只返回IEqualityComparer。嗯。請忽略以前的答案。我會編輯一個新的答案。 –