通过异构和类型安全的字典来发布

时间:2015-09-14 09:16:06

标签: c# dictionary iteration visitor-pattern

我需要一个像ditionary一样的容器,但数据类型(TValue)从一个键变为另一个键。

我还需要通过它进行迭代。

1 个答案:

答案 0 :(得分:2)

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

Wilka response is a good start

诀窍是将类型放入密钥中。

/// <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)
    {
    }
}

因此对字典的调用将具有通用值类型:

/// <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);
    }
}