2011-10-14 37 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