[英]Unable to constrain generic type
我不知道这里发生了什么。 我正在为Dictionary集合构建包装器。 这个想法是,当集合的大小很小时,它将使用普通的内存字典; 但是,当达到项目的阈值数量时,它将在内部切换到磁盘上的Dictionary(我使用的是ManagedEsent PersistentDictionary类)。
下面是磁盘版本的片段。 编译时,它失败并显示以下错误:
“类型'T_KEY'不能用作通用类型或方法'Microsoft.Isam.Esent.Collections.Generic.PersistentDictionary <TKey,TValue>'中的类型参数'TKey'。没有装箱转换或类型参数转换从“ T_KEY”到“ 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...
}
您的DiskDictionary
类需要指定T_KEY
实现IComparable<TKey>
:
class DiskDictionary<T_KEY, T_VALUE> : IHybridDictionary<T_KEY, T_VALUE>
where T_KEY : System.IComparable<T_KEY>
{
}
该接口既有通用版本又有非通用版本,您指定的是错误的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.