将标签附加到枚举值的最佳方法是什么

时间:2022-06-18 22:43:12

lets say I have an enum definition like so:

假设我有一个这样的枚举定义:

    public enum eCommentType
    {
        NormalComment = 0,
        OpenningComment = 1,
        StartProgressCommetn = 2,
        ClouserComment = 3,
        ReopennignComment = 4
    }

Now I want to present the enum option on a web page. I could just use a switch statement but then when i add a new value I will have to update my switch statement. What is the best way to append a label to each value or event better a resource id to support multilingual interface?

现在我想在网页上显示枚举选项。我可以使用switch语句,但是当我添加一个新值时,我将不得不更新我的switch语句。为每个值或事件附加标签更好的资源ID以支持多语言界面的最佳方法是什么?

P.S I use MVC for my current project but I would appreciate a general answer that could be used across many technologies i.e. a design pattern.

P.S我将MVC用于我当前的项目,但我希望能够在许多技术(即设计模式)中使用的一般答案。

4 个解决方案

#1


1  

event better a resource id to support multilingual interface?

事件更好的资源ID支持多语言界面?

You can get Get text from resources

您可以从资源中获取文本

var text = normalComment.GetName();


public static class EnumExtension
{
        public static string GetName(this eCommentType type)
        {
            return Strings.ResourceManager.GetString(type.ToString());
        }
}

#2


0  

The to string return the name of the enum or you can use the tag [Description("yournamehere")] for every enum and use that

to string返回枚举的名称,或者你可以为每个枚举使用标签[Description(“yournamehere”)]并使用它

public enum eCommentType
{
    [Description("my super normal comment")]
    NormalComment = 0,
    [Description("my super opening comment")]
    OpenningComment = 1,
    [Description("etc")]
    StartProgressCommetn = 2,
    [Description("etc")]
    ClouserComment = 3,
    [Description("etc")]
    ReopennignComment = 4
}

#3


0  

in the .net world you could solve this by adding attributes to your enum and by implementing a custom type converter that could use a ressource id from an attribute to translate the corresponding enum value into localized text

在.net世界中,您可以通过向枚举添加属性并实现自定义类型转换器来解决此问题,该转换器可以使用属性中的ressource id将相应的枚举值转换为本地化文本

this approach is basically the same as the one provided by Fabio Marcolini, except that a custom typeconverter will make the description localizable

这种方法与Fabio Marcolini提供的方法基本相同,不同之处在于自定义类型转换器将使描述可定位

#4


0  

If you need only descriptions of the items inside to show to a user, you can use:

如果您只需要向用户展示其中的项目,您可以使用:

Enum.GetValues , like :

Enum.GetValues,如:

var values = Enum.GetValues(typeof(eCommentType)); 

But still, MVC is not usually only about presentation, but also a binding between UI and a model, so usually you need to map values visible on the screen to actual values of eCommentType enum You will have to have some "mapper" that converts one data to another.

但是,MVC通常不只是关于表示,而是UI和模型之间的绑定,所以通常你需要将屏幕上可见的值映射到eCommentType枚举的实际值你必须有一些转换一个的“mapper”数据到另一个。

Yes, you can use also the same function that iterates over all available values and find requested one, after get's it's INT, but I would go for simple and clear switch/case, honestly, unless the enum values quantity becomes big.

是的,您可以使用相同的函数迭代所有可用值并找到所需的值,在得到它的INT之后,但我会选择简单明了的开关/情况,老实说,除非枚举值数量变大。

#1


1  

event better a resource id to support multilingual interface?

事件更好的资源ID支持多语言界面?

You can get Get text from resources

您可以从资源中获取文本

var text = normalComment.GetName();


public static class EnumExtension
{
        public static string GetName(this eCommentType type)
        {
            return Strings.ResourceManager.GetString(type.ToString());
        }
}

#2


0  

The to string return the name of the enum or you can use the tag [Description("yournamehere")] for every enum and use that

to string返回枚举的名称,或者你可以为每个枚举使用标签[Description(“yournamehere”)]并使用它

public enum eCommentType
{
    [Description("my super normal comment")]
    NormalComment = 0,
    [Description("my super opening comment")]
    OpenningComment = 1,
    [Description("etc")]
    StartProgressCommetn = 2,
    [Description("etc")]
    ClouserComment = 3,
    [Description("etc")]
    ReopennignComment = 4
}

#3


0  

in the .net world you could solve this by adding attributes to your enum and by implementing a custom type converter that could use a ressource id from an attribute to translate the corresponding enum value into localized text

在.net世界中,您可以通过向枚举添加属性并实现自定义类型转换器来解决此问题,该转换器可以使用属性中的ressource id将相应的枚举值转换为本地化文本

this approach is basically the same as the one provided by Fabio Marcolini, except that a custom typeconverter will make the description localizable

这种方法与Fabio Marcolini提供的方法基本相同,不同之处在于自定义类型转换器将使描述可定位

#4


0  

If you need only descriptions of the items inside to show to a user, you can use:

如果您只需要向用户展示其中的项目,您可以使用:

Enum.GetValues , like :

Enum.GetValues,如:

var values = Enum.GetValues(typeof(eCommentType)); 

But still, MVC is not usually only about presentation, but also a binding between UI and a model, so usually you need to map values visible on the screen to actual values of eCommentType enum You will have to have some "mapper" that converts one data to another.

但是,MVC通常不只是关于表示,而是UI和模型之间的绑定,所以通常你需要将屏幕上可见的值映射到eCommentType枚举的实际值你必须有一些转换一个的“mapper”数据到另一个。

Yes, you can use also the same function that iterates over all available values and find requested one, after get's it's INT, but I would go for simple and clear switch/case, honestly, unless the enum values quantity becomes big.

是的,您可以使用相同的函数迭代所有可用值并找到所需的值,在得到它的INT之后,但我会选择简单明了的开关/情况,老实说,除非枚举值数量变大。