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

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

问题描述

我怎样才能达到使用的枚举 .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天全站免登陆