将硬编码的switch语句转换为动态加载的多键一值查找 [英] Converting a hardcoded switch statement into a dynamically loaded many-keys-one-value lookup

查看:104
本文介绍了将硬编码的switch语句转换为动态加载的多键一值查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个好的收集/容器,可用于基本上存储具有多个掉线情况的切换语句的内容?我想,一种更准确的方法是,对于许多键一个值"的查找,一种好的C#解决方案是什么?我检查了System.Collections命名空间及其关联的命名空间,但没有发现特别适合于多键一值"的任何内容.

What is a good collection/container to use for basically storing the contents of a switch-statement with multiple fallthrough situations? I guess, a more accurate way is, what's a good C# solution for a "many-keys-one-value" lookup? I checked through the System.Collections namespace and its associated namespaces, but didn't find anything that particularly lended itself to many-keys-one-value.

最近,我为自己编写了一个小型控制台程序,以构造和验证Web应用程序所需的XML文件,以避免手动编写时发生的错误.但是对于最初的版本,我对一些稍微可变的元素进行了硬编码-特别是一些用于验证的字典和一个带有大量掉线选项的大型切换语句.尽管这将在未来几个月中起作用,但最终有必要更新这些硬编码元素,而这目前需要编辑源文件.为了避免这种情况,我决定将字典和切换语句信息存储在XML文件中(该文件最终以一种极其递归的方式演变为编写程序来构造和验证那些对我来说是XML文件).现在,将其存储在文件中时,当我检索数据时,不能像字典一样简单地重建switch语句.因此,我只需要模拟switch语句的效果,这基本上意味着我需要几个键"来引用相同的值".

Recently, I wrote myself a small console program to construct and validate the XML files I need for my web application, to avoid the errors that occur when manually writing it. But for the initial version, I hardcoded a number of elements which are slightly mutable - specifically a few dictionaries for validation and one large switch-statement that has a hefty set of fallthrough options. While this will work for quite a few months to come, it may be necessary to eventually update these hardcoded elements, which currently would require editing the source file. In the interest of avoiding this, I decided that I'd store the dictionary and switch-statement information in an XML file (which eventually evolved, in a horribly recursive trend, to writing a program to construct and validate those XML files for me). Now, in storing this in a file, when I retrieve the data I can't simply rebuild the switch statement like I can the dictionaries. So I simply need to simulate the effect of a switch statement, which basically means I need several "keys" to reference the same "value".

switch语句中的示例代码段.此方法会将特殊值输入转换为相应的ID,主要是为了节省试图记忆这些值的人的存储空间.基本上如图所示,我想要一个容器来存储全部输出单个值的几个键.

Example snippet from the switch statement. This method will convert special value inputs into a corresponding ID, mostly to save on human memory space trying to memorize the values. Basically as illustrated, I'd like a container to store the several keys that all output a single value.

    private static string DiscernRegistrationId(string str)
{
    string retVal = str;
    switch (str.ToLowerKey()) //shortcut extension for str.Replace(" ","").Replace(".","").ToLowerInvariant()
    {
        case "computerpackage":
        case "comppackage":
        case "cpackage":
        case "computerpkg":
        case "comppkg":
        case "cpkg":
            retVal = "0x0100C73692D109C741A1B8846141B47519C8";
            break;
        case "mobilepackage":
        case "mobpackage":
        case "mpackage":
        case "mobilepkg":
        case "mobpkg":
        case "mpkg":
            retVal = "0x010081F753F0A4A5412CB6A8E9F8CD193F58";
            break;
        //More cases follow
    }
    return retVal;
}

推荐答案

我认为您最好的选择是坚持使用内置的IDictionary<>类之一,然后重复这些值.在这种情况下,我实际上建议使用SortedDictionary<>,因为它按键顺序存储项目.如果您需要以与switch语句相同的视觉外观打印键,那将很有用.

I think your best option is to stick with one of the builtin IDictionary<> classes and just duplicate the values. I would actually recommend a SortedDictionary<> in this case since it stores items in order by the key. That would be useful if you needed to print out the keys in manner that gives the same visual appearance as the switch statement.

多次存储值对您来说应该不是问题,因为它们是作为引用类型的字符串.当然,您将重复引用同一字符串,但这并不是您要复制整个字符串的内容或其他任何内容.

Storing the values multiple times should not be an issue for you since they are strings which are reference types. Sure, you will have duplicate references to the same string, but it is not like you are duplicating the entire string's contents or anything.

要指出的另一件事是,我相信编译器实际上是在后台将switch语句转换为哈希表.这意味着它可能正在使用我刚刚建议的相同策略.拆解一下,看看.

Another thing to point out is that I believe the compiler is actually converting that switch statement into hashtable behind the scenes. That means it is probably using the same strategy I just recommended. Disassemble it and take a look.

这篇关于将硬编码的switch语句转换为动态加载的多键一值查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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