枚举值如何与空格? [英] How to have enum values with spaces?

查看:104
本文介绍了枚举值如何与空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 .NET 中使用枚举来实现以下内容?我想对包含空格的每个值进行描述。

How can I achieve the following using enums in .NET? I would like to have descriptions for each value that include spaces.

public enum PersonGender
    {
        Unknown = 0,
        Male = 1,
        Female = 2,
        Intersex = 3,
        Indeterminate = 3,
        Non Stated = 9,
        Inadequately Described = 9
    }

我想要选择是否使用描述或者整数,每次我使用这种类型的值。

I would like to be able to choose whether to use either the description or integer each time I use a value of this type.

推荐答案

不可以,但是你可以将属性附加到枚举成员。 EnumMemberAttribute 是为了您所描述的目的而设计的。

No that's not possible, but you can attach attributes to enum members. The EnumMemberAttribute is designed exactly for the purpose you described.

public enum PersonGender
{
    Unknown = 0,
    Male = 1,
    Female = 2,
    Intersex = 3,
    Indeterminate = 3,

    [EnumMember(Value = "Not Stated")]
    NonStated = 9,

    [EnumMember(Value = "Inadequately Described")]
    InadequatelyDescribed = 9
}

有关如何使用 EnumMemberAttribute 转换字符串的更多信息到枚举值,请参见这个线程

For more information on how to use the EnumMemberAttribute to convert strings to enum values, see this thread.

这篇关于枚举值如何与空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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