字典瓦特/空键? [英] Dictionary w/ null key?

查看:127
本文介绍了字典瓦特/空键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,为什么的不词典< TKEY的,TValue> 支持单一的空键

Firstly, why doesn't Dictionary<TKey, TValue> support a single null key?

其次,有一个现有的字典的集合呢?

Secondly, is there an existing dictionary-like collection that does?

我要存储一个空或丢失或默认的System.Type ,认为将工作做好这一点。

I want to store an "empty" or "missing" or "default" System.Type, thought null would work well for this.

更具体地说,我已经写了这个类:

More specifically, I've written this class:

class Switch
{
    private Dictionary<Type, Action<object>> _dict;

    public Switch(params KeyValuePair<Type, Action<object>>[] cases)
    {
        _dict = new Dictionary<Type, Action<object>>(cases.Length);
        foreach (var entry in cases)
            _dict.Add(entry.Key, entry.Value);
    }

    public void Execute(object obj)
    {
        var type = obj.GetType();
        if (_dict.ContainsKey(type))
            _dict[type](obj);
    }

    public static void Execute(object obj, params KeyValuePair<Type, Action<object>>[] cases)
    {
        var type = obj.GetType();

        foreach (var entry in cases)
        {
            if (entry.Key == null || type.IsAssignableFrom(entry.Key))
            {
                entry.Value(obj);
                break;
            }
        }
    }

    public static KeyValuePair<Type, Action<object>> Case<T>(Action action)
    {
        return new KeyValuePair<Type, Action<object>>(typeof(T), x => action());
    }

    public static KeyValuePair<Type, Action<object>> Case<T>(Action<T> action)
    {
        return new KeyValuePair<Type, Action<object>>(typeof(T), x => action((T)x));
    }

    public static KeyValuePair<Type, Action<object>> Default(Action action)
    {
        return new KeyValuePair<Type, Action<object>>(null, x => action());
    }
}

有关类型的开关。有使用它有两种方式:

For switching on types. There are two ways to use it:


  1. 静态。只需拨打 Switch.Execute(yourObject,Switch.Case&LT; YourType&GT;(X =&GT; x.Action()))

  2. precompiled。创建一个开关,然后用 switchInstance.Execute(yourObject)
  3. 在以后使用
  1. Statically. Just call Switch.Execute(yourObject, Switch.Case<YourType>(x => x.Action()))
  2. Precompiled. Create a switch, and then use it later with switchInstance.Execute(yourObject)

伟大工程的除了的当您尝试默认情况下,添加到precompiled版(null参数除外)。

Works great except when you try to add a default case to the "precompiled" version (null argument exception).

推荐答案

它只是打我你最好的答案很可能是只跟踪默认情况下,是否已被定义为:

It just hit me that your best answer is probably to just keep track of whether a default case has been defined:

class Switch
{
    private Dictionary<Type, Action<object>> _dict;
    private Action<object> defaultCase;

    public Switch(params KeyValuePair<Type, Action<object>>[] cases)
    {
        _dict = new Dictionary<Type, Action<object>>(cases.Length);
        foreach (var entry in cases)
            if (entry.Key == null)
                defaultCase = entry.Value;
            else
                _dict.Add(entry.Key, entry.Value);
    }

    public void Execute(object obj)
    {
        var type = obj.GetType();
        if (_dict.ContainsKey(type))
            _dict[type](obj);
        else if (defaultCase != null)
            defaultCase(obj);
    }

...

类的整个其余部分将保持不变。

The whole rest of your class would remain untouched.

这篇关于字典瓦特/空键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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