遍历异构和类型安全的字典

本文关键字:字典 类型安全 异构 遍历 | 更新日期: 2023-09-27 18:12:33

我需要一个像字典一样工作的容器,但其中数据类型(TValue)从一个键更改到另一个键。

我还需要遍历它

遍历异构和类型安全的字典

对于异构和类型安全的字典部分

威尔卡的反应是个好的开始。

关键是把类型放在键中。
/// <summary>
/// Base class for all dictionary key.
/// 
/// <remarks>The key name is REALLY usefull for debug purpose.</remarks>
/// </summary>
abstract class HeterogeneousDictionaryKeyBase
{
    readonly string _name;
    protected HeterogeneousDictionaryKeyBase(string name)
    {
        _name = name;
    }
    public override string ToString()
    {
        return _name;
    }
}
sealed class HeterogeneousDictionaryKey<TValue> : HeterogeneousDictionaryKeyBase
{
    public HeterogeneousDictionaryKey(string name)
        : base(name)
    {
    }
}

所以对dictionary的调用将有一个泛型值类型:

/// <summary>
/// <remarks>The [] operator can not be generic, so we implement it has a getter and a setter</remarks>
/// </summary>
class HeterogeneousDictionary
{
    private readonly Dictionary<HeterogeneousDictionaryKeyBase, object> _dictionary = new Dictionary<HeterogeneousDictionaryKeyBase, object>();
    public void Add<TValue>(HeterogeneousDictionaryKey<TValue> key, TValue value)
    {
        _dictionary.Add(key, value);
    }
    public TValue Get<TValue>(HeterogeneousDictionaryKey<TValue> key)
    {
        return (TValue)_dictionary[key];
    }
    public void Set<TValue>(HeterogeneousDictionaryKey<TValue> key, TValue value)
    {
        _dictionary[key] = value;
    }
    public bool TryGetValue<TValue>(HeterogeneousDictionaryKey<TValue> key, out TValue value)
    {
        object result;
        if (_dictionary.TryGetValue(key, out result) && result is TValue)
        {
            value = (TValue)result;
            return true;
        }
        value = default(TValue);
        return false;
    }
}

用法很简单:

var dictionary = new HeterogeneousDictionary();
var keyName = new HeterogeneousDictionaryKey<string>("keyName");
var keyAge = new HeterogeneousDictionaryKey<int>("keyAge");
dictionary.Set(keyName, "Orace");
dictionary.Set(keyAge, 8);
...
var name = dictionary.Get(keyName);
var age = dictionary.Get(keyAge);

对于迭代部分

针对字典键的访问模式可以达到目的。

首先是访问者界面:

interface IHeterogeneousDictionaryKeyVisitor
{
    void Visit<TValue>(HeterogeneousDictionaryKey<TValue> key);
}

然后我们让HeterogeneousDictionaryKey合作:

abstract class HeterogeneousDictionaryKeyBase
{
    ...
    public abstract void Accept(IHeterogeneousDictionaryKeyVisitor visitor);
    ...
}
sealed class HeterogeneousDictionaryKey<TValue> : HeterogeneousDictionaryKeyBase
{
    ...
    public override void Accept(IHeterogeneousDictionaryKeyVisitor visitor)
    {
        visitor.Visit(this);
    }
}

现在我们可以公开HeterogeneousDictionary键:

class HeterogeneousDictionary
{
    ...
    public Dictionary<HeterogeneousDictionaryKeyBase, object>.KeyCollection Keys
    {
        get { return _dictionary.Keys; }
    }
    ...
}

这就是一切。

下面是安全地将一个字典复制到另一个 的用法示例
class DictionaryCopier : IHeterogeneousDictionaryKeyVisitor
{
    readonly HeterogeneousDictionary _source;
    readonly HeterogeneousDictionary _destination;
    public DictionaryCopier(HeterogeneousDictionary source, HeterogeneousDictionary destination)
    {
        _source = source;
        _destination = destination;
    }
    public void PerformCopy()
    {
        foreach (var key in _source.Keys)
        {
            // See you soon.
            key.Accept(this);
        }
    }
    /// <summary>
    /// We fall back here with a typed key.
    /// </summary>
    public void Visit<TValue>(HeterogeneousDictionaryKey<TValue> key)
    {
        // Here the value is typed.
        var value = _source.Get(key);
        _destination.Add(key, value);
    }
}