C# 实现自己的字典的基类

C# 实现自己的字典的基类,c#,dictionary,C#,Dictionary,如果我需要自己的IList(T),但不想从头开始实现它,我可以从 System.Collections.ObjectModel.Collection<T> System.Collections.ObjectModel.Collection 这将允许我自定义添加、删除和清除操作。 但是如果我想要完全一样的东西,我该怎么办呢 IDictionary<T1, T2> IDictionary ?为什么不创建一个IDictionary的实现,将方法调用委托给内部持有的字典

如果我需要自己的IList(T),但不想从头开始实现它,我可以从

System.Collections.ObjectModel.Collection<T>
System.Collections.ObjectModel.Collection
这将允许我自定义添加、删除和清除操作。
但是如果我想要完全一样的东西,我该怎么办呢

IDictionary<T1, T2> 
IDictionary

为什么不创建一个
IDictionary
的实现,将方法调用委托给内部持有的
字典
?然后你可以以任何你认为合适的方式改变行为

public class MyCustomDictionary<TKey,TVal>:IDictionary<TKey,TVal>
{
    private Dictionary<TKey, TVal> dictionary;

    public MyCustomDictionary()
    {
        dictionary=new Dictionary<TKey, TVal>();
    }

    public void CopyTo(Array array, int index)
    {
        ((ICollection) dictionary).CopyTo(array, index);
    }

    public object SyncRoot
    {
        get { return ((ICollection) dictionary).SyncRoot; }
    }

    public bool IsSynchronized
    {
        get { return ((ICollection) dictionary).IsSynchronized; }
    }

    public bool Contains(object key)
    {
        return ((IDictionary) dictionary).Contains(key);
    }

    public void Add(object key, object value)
    {
        ((IDictionary) dictionary).Add(key, value);
    }

    public void Remove(object key)
    {
        ((IDictionary) dictionary).Remove(key);
    }

    public object this[object key]
    {
        get { return dictionary[(TKey) key]; }
        set { dictionary[(TKey) key] = (TVal) value; }
    }

    public bool IsFixedSize
    {
        get { return ((IDictionary) dictionary).IsFixedSize; }
    }

    public void Add(TKey key, TVal value)
    {
        dictionary.Add(key, value);
    }

    public void Clear()
    {
        dictionary.Clear();
    }

    public bool Contains(KeyValuePair<TKey, TVal> item)
    {
        TVal v;
        return (dictionary.TryGetValue(item.Key, out v) && v.Equals(item.Key));
    }

    public void CopyTo(KeyValuePair<TKey, TVal>[] array, int arrayIndex)
    {
        ((ICollection<KeyValuePair<TKey, TVal>>)dictionary)
            .CopyTo(array,arrayIndex);
    }

    public bool Remove(KeyValuePair<TKey, TVal> item)
    {
        if (Contains(item))
        {
            dictionary.Remove(item.Key);
            return true;
        }
        return false;
    }

    public bool ContainsKey(TKey key)
    {
        return dictionary.ContainsKey(key);
    }

    public bool ContainsValue(TVal value)
    {
        return dictionary.ContainsValue(value);
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        dictionary.GetObjectData(info, context);
    }

    public void OnDeserialization(object sender)
    {
        dictionary.OnDeserialization(sender);
    }

    public bool Remove(TKey key)
    {
        return dictionary.Remove(key);
    }

    public bool TryGetValue(TKey key, out TVal value)
    {
        return dictionary.TryGetValue(key, out value);
    }

    public IEqualityComparer<TKey> Comparer
    {
        get { return dictionary.Comparer; }
    }

    public int Count
    {
        get { return dictionary.Count; }
    }

    public bool IsReadOnly { get; private set; }

    public TVal this[TKey key]
    {
        get { return dictionary[key]; }
        set { dictionary[key] = value; }
    }

    public ICollection<TKey> Keys
    {
        get { return dictionary.Keys; }
    }

    public ICollection<TVal> Values
    {
        get { return dictionary.Values; }
    }

    public IEnumerator<KeyValuePair<TKey, TVal>> GetEnumerator()
    {
        return dictionary.GetEnumerator();
    }

    public void Add(KeyValuePair<TKey,TVal> item)
    {
        dictionary.Add(item.Key,item.Value);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
公共类MyCustomDictionary:IDictionary
{
私人字典;
公共MyCustomDictionary()
{
字典=新字典();
}
public void CopyTo(数组,int索引)
{
((ICollection)字典).CopyTo(数组,索引);
}
公共对象同步根
{
获取{return((ICollection)dictionary.SyncRoot;}
}
公共布尔值是同步的
{
获取{return((ICollection)字典).IsSynchronized;}
}
公共布尔包含(对象键)
{
return((IDictionary)dictionary).Contains(key);
}
公共无效添加(对象键、对象值)
{
((IDictionary)字典)。添加(键、值);
}
公共无效删除(对象键)
{
((IDictionary)字典)。删除(键);
}
公共对象此[对象密钥]
{
获取{return dictionary[(TKey)key];}
设置{dictionary[(TKey)key]=(TVal)值;}
}
公共图书馆是固定大小的
{
获取{return((IDictionary)dictionary).IsFixedSize;}
}
公共无效添加(TKey、TVal值)
{
添加(键、值);
}
公共空间清除()
{
dictionary.Clear();
}
public bool包含(KeyValuePair项)
{
TVal v;
返回(dictionary.TryGetValue(item.Key,out v)和&v.Equals(item.Key));
}
public void CopyTo(KeyValuePair[]数组,int-arrayIndex)
{
((ICollection)字典)
.CopyTo(数组、数组索引);
}
公共布尔删除(KeyValuePair项)
{
如果(包含(项目))
{
删除(item.Key);
返回true;
}
返回false;
}
公共bool ContainsKey(TKey)
{
返回dictionary.ContainsKey(key);
}
公共布尔值包含值(TVal值)
{
返回dictionary.ContainsValue(值);
}
public void GetObjectData(SerializationInfo信息、StreamingContext上下文)
{
GetObjectData(信息、上下文);
}
公共序列化(对象发送方)
{
字典.序列化(发送方);
}
公用门移除(TKey)
{
返回字典。删除(键);
}
公共bool TryGetValue(TKey键、out TVal值)
{
返回dictionary.TryGetValue(key,out值);
}
公共IEqualityComparer比较器
{
获取{return dictionary.Comparer;}
}
公共整数计数
{
获取{return dictionary.Count;}
}
公共bool是只读的{get;private set;}
公共TVal本[TKey]
{
获取{返回字典[key];}
设置{dictionary[key]=value;}
}
公共ICollection密钥
{
获取{return dictionary.Keys;}
}
公共ICollection值
{
获取{返回dictionary.Values;}
}
公共IEnumerator GetEnumerator()
{
返回dictionary.GetEnumerator();
}
公共作废添加(KeyValuePair项)
{
dictionary.Add(item.Key,item.Value);
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}

IDictionary只指定了六种方法。您不想自己实现哪些功能?使用
字典
(或
SortedDictionary
等)作为专用字段来实现接口
IDictionary
。不鼓励从
Dictionary
继承…@DavidArno:根据,
IDictionary
指定了5个属性和10个方法,这还不包括需要显式接口实现的其他属性和方法,例如。您希望以何种方式自定义它们?除非我们知道您的意图,否则无法向您提供建议。@O.R.Mapper,我的错。我没有正确地阅读这个问题,错过了OP引用的是IDictionary的通用版本这一事实。这真的有很大的不同!顺便说一句,Resharper在晚餐时会吃这种问题。是的,这是最明显的解决方法,但System.Collections.ObjectModel.Collection的存在和缺少类似的IDictionary对我来说似乎不一致。嗯,
字典
ICollection
的一种实现,但我敢说,用这个作为出发点是错误的。