无法限制通用类型 [英] Unable to constrain generic type

查看:47
本文介绍了无法限制通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这里发生了什么.我正在为Dictionary集合构建包装器.这个想法是,当集合的大小很小时,它将使用一个普通的内存字典;但是,当达到项目的阈值数量时,它将在内部切换到磁盘字典(我使用的是ManagedEsent PersistentDictionary类).

I can't figure out what's happening here. I'm building a wrapper for a Dictionary collection. The idea is that, when the size of the collection is small, it will use a normal in-memory Dictionary; but, when a threshold number of items is reached, it will internally switch to an on-disk Dictionary (I'm using the ManagedEsent PersistentDictionary class).

下面是磁盘版本的片段.编译时,它失败并显示以下错误:

A snippet of the on-disk version is below. When compiling, it fails with the following error:

类型'T_KEY'不能用作类型参数'TKey'通用类型或方法"Microsoft.Isam.Esent.Collections.Generic.PersistentDictionary< TKey,TValue>".没有来自的装箱转换或类型参数转换从"T_KEY"到"System.IComparable< T_KEY>"."

"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>'."

因此我将类定义修改为:

So I modified the class definition to be:

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

认为可以解决问题,但事实并非如此.我也尝试限制IHybridDictionary的定义,但这没有任何效果.有什么想法吗?

thinking that would do the trick, but it didn't. I tried constraining the definition IHybridDictionary too but that didn't have any effect. Any thoughts on what's going on?

DiskDictionary的原始定义:

Original definition of 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> >:

Your DiskDictionary class need to specify that T_KEY implements IComparable<TKey> :

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

此接口既有通用版本又有非通用版本,而您指定的是错误的版本.

There is both a generic and a non generic version of this interface and you were specifying the wrong one.

这篇关于无法限制通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆