访问 C# 应用程序中的硬编码数据

发布于 2024-09-05 09:15:19 字数 314 浏览 2 评论 0原文

我试图避免在 .net 2.0 即将成为 3.5 应用程序中进行硬编码。

我有一个大型枚举,我希望将 1 到 1 映射到一组字符串。每个枚举值还将映射到指示操作的 2 个值中的 1 个。现有的代码使用一个大的 switch 语句来完成此操作,但这对我来说似乎很难看。

有没有更好的方法来存储和访问数据?

我考虑过 resx 文件,但是当您考虑到设计器文件包含同样多的硬编码值时,这似乎有点毫无意义。

在程序集中嵌入 xml 文件是个好主意吗?

一个大的 switch 语句是否没有看起来那么糟糕?

有更好的解决方案吗?

I'm trying to avoid hardcoding in a .net 2.0 soon to be 3.5 application.

I have a large enumeration which I wish to map 1 to 1 to a set of strings. Each enumerated value will also map to 1 of 2 values indicating an action. The existing code does this with a big switch statement but this seems ugly to me.

Is there a better way of storing and accessing the data?

I've thought about resx files but when you consider that the designer file contains just as many hardcoded values it seems a little pointless.

Is embedding an xml file in the assembly a good idea?

Is a big switch statement not as bad as it seems?

Is there a better solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美人如玉 2024-09-12 09:15:20

您可以使用枚举描述属性为每个枚举值分配一个字符串。我不知道它是否比 switch 语句更有效,但它确实具有在定义枚举的代码中的同一位置分配字符串值的优点。

CodeProject 在此处有一篇文章。 此处还有一个很好的示例。

You can use the Enum description attribute to assign a string to each enum value. I don't know if it's more efficient than a switch statement but it does have the advantage of assigning the string value in the same place in code that the enum is defined.

CodeProject has an article here. There's also a good example here.

灵芸 2024-09-12 09:15:20

尽管 switch 语句多么丑陋,但它是将枚举映射到字符串的最有效方法。嵌入 XML 文件只会增加程序集的大小,并且需要更长的时间才能获取所需的字符串值。

Despite how ugly a switch statement is its the most efficient way to map enums to strings. Embedding an XML file will just bloat the size of your assembly and it will take much longer to get the string value you need.

只涨不跌 2024-09-12 09:15:20

另请参阅此问题的答案问题。

恕我直言,大的 switch 语句总是有代码味道,并且无法维护。

Also see the answers to this question.

IMHO, big switch statements are ALWAYS a code smell, and are impossible to maintain.

梦亿 2024-09-12 09:15:20

在这种情况下使用枚举就是试图将方钉装入圆孔中。而 XML 对于您想要做的事情来说就太过分了。

请改用字典。这样,就有一个直接的 'key =>; value' 关系,并且 value 可以是任何东西。

对于 1-1 字符串关系,请使用此:

Dictionary<string, string> dictA = new Dictionary<string, string>();
dictA.Add("string1", "value1");
dictA.Add("string2", "value2");
dictA.Add("string3", "value3");

要访问字典的所有值,请使用此:

foreach (KeyValuePair<string, string> item in dictA)
{   
    Console.WriteLine("Key: {0}; Value: {1};", item.Key, item.Value);
}

如果您需要 1-2 1-3 或 1-n 字符串关系,请使用this:

Dictionary<string, List<string>> dictB = new Dictionary<string, List<string>>();
dictB.Add("string1", new List<string>() {"value1A", "value1B"});
dictB.Add("string2", new List<string>() {"value2A", "value2B"});
dictB.Add("string3", new List<string>() {"value3A", "value3B"});

要访问字典的所有值,请使用此:

foreach (KeyValuePair<string, List<string>> item in dictB)
{   
    Console.WriteLine("Key: {0}; Values: {1}, {2};", item.Key, item.Value[0], item.Value[1]);
}

如果需要交错数组关系,请使用此:

Dictionary<string, List<string>> dictB = new Dictionary<string, List<string>>();
dictB.Add("string1", new List<string>() {"value1A", "value1B, value1C"});
dictB.Add("string2", new List<string>() {"value2A", "value2B, value2C, value2D"});
dictB.Add("string3", new List<string>() {"value3A", "value3B"});

要访问字典的所有值,请使用此:

foreach (KeyValuePair<string, List<string>> item in dictB)
{   
    string values = "";
    int valLen = ((List<string>)item.Value).Count;
    for (int i = 0; i < valLen; i++)
    {
        if (i == (valLen - 1))
        {
            values += item.Value[i] + "";
        }
        else
        {
            values += item.Value[i] + ", ";
        }
    }
    Console.WriteLine("Key: {0}; Values: {1};", item.Key, values);
}

要获取特定键的值执行如下操作:

string match = "string2";
if (dictD.ContainsKey(match))
{
    Console.WriteLine("The value for Key='{0}' is: '{1}'", match, dictD[match]);
}

枚举只不过是一个美化的字典。这就是为什么您不会在许多其他语言(尤其是动态类型语言)中看到它们。

Using an enum in this case is trying to fit a square peg in a round hole. Whereas XML is overkill for what you're trying to do.

Use a dictionary instead. That way, there is a direct 'key => value' relationship and the value can be anything.

For the 1-1 string string relationship use this:

Dictionary<string, string> dictA = new Dictionary<string, string>();
dictA.Add("string1", "value1");
dictA.Add("string2", "value2");
dictA.Add("string3", "value3");

To access all of the values of the dictionary use this:

foreach (KeyValuePair<string, string> item in dictA)
{   
    Console.WriteLine("Key: {0}; Value: {1};", item.Key, item.Value);
}

If you need a 1-2 1-3 or 1-n string relationship use this:

Dictionary<string, List<string>> dictB = new Dictionary<string, List<string>>();
dictB.Add("string1", new List<string>() {"value1A", "value1B"});
dictB.Add("string2", new List<string>() {"value2A", "value2B"});
dictB.Add("string3", new List<string>() {"value3A", "value3B"});

To access all of the values of the dictionary use this:

foreach (KeyValuePair<string, List<string>> item in dictB)
{   
    Console.WriteLine("Key: {0}; Values: {1}, {2};", item.Key, item.Value[0], item.Value[1]);
}

If you need a staggered array relationship use this:

Dictionary<string, List<string>> dictB = new Dictionary<string, List<string>>();
dictB.Add("string1", new List<string>() {"value1A", "value1B, value1C"});
dictB.Add("string2", new List<string>() {"value2A", "value2B, value2C, value2D"});
dictB.Add("string3", new List<string>() {"value3A", "value3B"});

To access all of the values of the dictionary use this:

foreach (KeyValuePair<string, List<string>> item in dictB)
{   
    string values = "";
    int valLen = ((List<string>)item.Value).Count;
    for (int i = 0; i < valLen; i++)
    {
        if (i == (valLen - 1))
        {
            values += item.Value[i] + "";
        }
        else
        {
            values += item.Value[i] + ", ";
        }
    }
    Console.WriteLine("Key: {0}; Values: {1};", item.Key, values);
}

To get the value for a specific key do something like this:

string match = "string2";
if (dictD.ContainsKey(match))
{
    Console.WriteLine("The value for Key='{0}' is: '{1}'", match, dictD[match]);
}

An Enums is nothing but a glorified Dictionary. That's why you won't see them in a lot of other languages (especially dynamically typed languages).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文