ASP.NET 中的枚举和字符串值

发布于 2024-09-02 08:45:13 字数 301 浏览 1 评论 0原文

我正在寻找一些有关枚举和检索关联字符串值的最佳实践建议。鉴于此:

public enum Fruits {
    Apple,
    Orange,
    Grapefruit,
    Melon
}

获取名称的相关字符串值的最佳方法是什么?例如。 “Grapefruit”,假设字符串值可能与枚举中的表示形式不匹配。例如“Casaba Melon”

我目前的想法是函数接受枚举并返回字符串,但这是否意味着对字符串值进行硬编码(我不喜欢这样做)?使用可以通过枚举检索字符串的资源文件是否过于严厉?

I'm looking for some best practice advice on enumerations and retrieving an associated string value. Given this:

public enum Fruits {
    Apple,
    Orange,
    Grapefruit,
    Melon
}

What is the best way to get a related string value of the name? Eg. "Grapefruit", given that the string value may not match the representation in the enumeration. eg "Casaba Melon"

My current thinking is function accepting an enum and returning a string, but would that not mean hard coding the string values (which I prefer not to do)? Is using a resources file where the string can be retrieved via the enumeration too heavy handed?

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

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

发布评论

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

评论(2

时光清浅 2024-09-09 08:45:13

要回答您的问题,您可以用属性装饰枚举,以便为它们提供正确的显示字符串。以下是一些示例

这种方法的主要限制是,如果您需要国际化您的应用程序,我不知道有什么方法可以使属性字符串根据线程区域设置更改值(或无论您使用什么方式来区分区域设置)。

To answer your question, you can decorate your enums with attributes to give them proper display string. Here are some examples

The main limitation of this approach is if you ever need to internationalize your application, I don't know of a way to make attribute strings change value based on thread locale (or whatever way you use to distinguish locales).

落叶缤纷 2024-09-09 08:45:13

R0MANARMY已经给出了一个非常好的解决方案。我将提供一种替代方案,虽然不太好,但仍然是一种。你也许可以让这种文化敏感变得更容易。

假设你有枚举,

public enum NicePeople
{
    SomeGuy,
    SomeOtherGuy
}

然后你可以创建一个像这样的扩展方法

public static class MyExtensions
{
    public static string GetName(this NicePeople tst)
    {
        switch (tst)
        {
            case NicePeople.SomeGuy:
                return "Some Nice guy";
            case NicePeople.SomeOtherGuy:
                return "Another Nice Guy";
            default:
                throw new Exception("Naw");
        }
    }
}

并得到你的连环杀手的名字,如下所示

NicePeople.SomeGuy.GetName()

R0MANARMY has already given a very nice solution. I'll provide this alternative, less nice, one though still. You can probably make this culture sensitive easier.

Say you have the enum

public enum NicePeople
{
    SomeGuy,
    SomeOtherGuy
}

You can then make an extension method like this

public static class MyExtensions
{
    public static string GetName(this NicePeople tst)
    {
        switch (tst)
        {
            case NicePeople.SomeGuy:
                return "Some Nice guy";
            case NicePeople.SomeOtherGuy:
                return "Another Nice Guy";
            default:
                throw new Exception("Naw");
        }
    }
}

And get your serial killers name like this

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