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

问题描述

首先,为什么为什么 Dictionary< 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 ,认为 null 可以很好地解决此问题。

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< YourType>(x => x.Action()))

  2. 预编译。创建一个开关,然后与 switchInstance.Execute(yourObject)

  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)

当您尝试将默认大小写添加到预编译版本(空参数异常)时,效果很好。

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.

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

10-27 00:34