2011-10-14 88 views
2

我無法弄清楚這裏發生了什麼。我正在構建Dictionary集合的包裝器。這個想法是,當集合的大小很小時,它將使用正常的內存中字典;但是,當達到閾值數量的項目時,它將在內部切換到磁盤上的字典(我正在使用ManagedEsent PersistentDictionary類)。無法限制泛型類型

以下是磁盤版本的一個片段。編譯時,它失敗,出現以下錯誤:

"The type 'T_KEY' cannot be used as type parameter 'TKey' in the generic type or method 'Microsoft.Isam.Esent.Collections.Generic.PersistentDictionary<TKey,TValue>'. There is no boxing conversion or type parameter conversion from 'T_KEY' to 'System.IComparable<T_KEY>'."

所以我修改類的定義是:

class DiskDictionary<T_KEY, T_VALUE> : IHybridDictionary<T_KEY, T_VALUE> 
    where T_KEY : System.IComparable 

思想,會做的伎倆,但事實並非如此。我試圖限制IHybridDictionary的定義,但沒有任何效果。對發生什麼事情有任何想法?

DiskDictionary的

最初的定義:

class DiskDictionary<T_KEY, T_VALUE> : IHybridDictionary<T_KEY, T_VALUE> 
{ 
    string dir; 
    PersistentDictionary<T_KEY, T_VALUE> d; 

    public DiskDictionary(string dir) 
    { 
     this.dir = dir; 
     //d = new PersistentDictionary<T_KEY, T_VALUE>(dir); 
    } 

    ... some other methods... 
} 

回答

4

DiskDictionary類需要指定T_KEY實現IComparable<TKey>

class DiskDictionary<T_KEY, T_VALUE> : IHybridDictionary<T_KEY, T_VALUE> 
    where T_KEY : System.IComparable<T_KEY> 
{ 
} 

有兩個通用的和這個界面的非通用版本和你指定錯誤的。

+0

啊,非常感謝。我最初嘗試過,並沒有奏效,但我沒有通過其他類定義來傳播它。 – Gadzooks34