如何使用空间的枚举值?(复制)

时间:2022-06-20 21:45:01

This question already has an answer here:

这个问题已经有了答案:

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

如何在。net中使用枚举实现以下目标?我希望对包含空格的每个值都有描述。

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.

我希望能够在每次使用这种类型的值时,选择使用描述还是整数。

3 个解决方案

#1


39  

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

不,这是不可能的,但是您可以将属性附加到enum成员。EnumMemberAttribute的设计完全符合您所描述的目的。

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
}

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

有关如何使用枚举属性将字符串转换为枚举值的更多信息,请参见此线程。

#2


5  

This is easy. Create an extension method for your string that returns a formatted string based on your coding convention. You can use it in lots of places, not just here. This one works for camelCase and TitleCase.

这是很容易的。为您的字符串创建一个扩展方法,该方法返回基于编码约定的格式化字符串。你可以在很多地方使用它,不仅仅是在这里。这款适用于camelCase和TitleCase。

    public static String ToLabelFormat(this String s)
    {
        var newStr = Regex.Replace(s, "(?<=[A-Z])(?=[A-Z][a-z])", " ");
        newStr = Regex.Replace(newStr, "(?<=[^A-Z])(?=[A-Z])", " ");
        newStr = Regex.Replace(newStr, "(?<=[A-Za-z])(?=[^A-Za-z])", " ");

        return newStr;
    }

#3


1  

var assembly = Assembly.LoadFrom("ResourcesLib.DLL");            
var resourceManager =
new ResourceManager("ResourcesLib.EnumDescriptions", assembly);                        

var lst = Enum.GetValues(typeof(PersonGender)).Cast<PersonGender>().ToList();
foreach (var gender in lst)
{
  Console.WriteLine(gender); // Name
  Console.WriteLine((int)gender); //Int Value
  Console.WriteLine(resourceManager.GetString(gender.ToString()));//localized Resorce
}          

So spaces may reside in localized resource ...

因此,空间可能驻留在本地化资源中……

#1


39  

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

不,这是不可能的,但是您可以将属性附加到enum成员。EnumMemberAttribute的设计完全符合您所描述的目的。

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
}

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

有关如何使用枚举属性将字符串转换为枚举值的更多信息,请参见此线程。

#2


5  

This is easy. Create an extension method for your string that returns a formatted string based on your coding convention. You can use it in lots of places, not just here. This one works for camelCase and TitleCase.

这是很容易的。为您的字符串创建一个扩展方法,该方法返回基于编码约定的格式化字符串。你可以在很多地方使用它,不仅仅是在这里。这款适用于camelCase和TitleCase。

    public static String ToLabelFormat(this String s)
    {
        var newStr = Regex.Replace(s, "(?<=[A-Z])(?=[A-Z][a-z])", " ");
        newStr = Regex.Replace(newStr, "(?<=[^A-Z])(?=[A-Z])", " ");
        newStr = Regex.Replace(newStr, "(?<=[A-Za-z])(?=[^A-Za-z])", " ");

        return newStr;
    }

#3


1  

var assembly = Assembly.LoadFrom("ResourcesLib.DLL");            
var resourceManager =
new ResourceManager("ResourcesLib.EnumDescriptions", assembly);                        

var lst = Enum.GetValues(typeof(PersonGender)).Cast<PersonGender>().ToList();
foreach (var gender in lst)
{
  Console.WriteLine(gender); // Name
  Console.WriteLine((int)gender); //Int Value
  Console.WriteLine(resourceManager.GetString(gender.ToString()));//localized Resorce
}          

So spaces may reside in localized resource ...

因此,空间可能驻留在本地化资源中……