C# 具有dropdownlist的空格属性的枚举

C# 具有dropdownlist的空格属性的枚举,c#,enums,C#,Enums,因为我在enum中没有空间,我正在尝试这样做,但似乎不喜欢它 public enum EnumState { NewYork, NewMexico } public EnumState State { get { return EnumState ; } set { if (EnumState .ToString() == "NewYork"

因为我在enum中没有空间,我正在尝试这样做,但似乎不喜欢它

public enum EnumState
{
    NewYork,
    NewMexico
}


public EnumState State
    {
        get
        {
            return EnumState ;
        }
        set
        {
            if (EnumState .ToString() == "NewYork".ToString())
            {
                value = "New York".ToString();
                EnumState = value;
            }
        }
    }

我看到通常通过在枚举成员上放置
[StringValue(“newyork”)]
属性来处理这个问题。一个快速的谷歌搜索会返回一个非常好的方法

基本上使属性类:

public class StringValueAttribute : Attribute {

    public string StringValue { get; protected set; }

    public StringValueAttribute(string value) {
        this.StringValue = value;
    }
}
以及访问它的扩展方法:

   public static string GetStringValue(this Enum value) {
        // Get the type
        Type type = value.GetType();

        // Get fieldinfo for this type
        FieldInfo fieldInfo = type.GetField(value.ToString());

        // Get the stringvalue attributes
        StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
            typeof(StringValueAttribute), false) as StringValueAttribute[];

        // Return the first if there was a match, or enum value if no match
        return attribs.Length > 0 ? attribs[0].StringValue : value.ToString();
    }
那么您的枚举将如下所示:

public enum EnumState{
  [StringValue("New York")]
  NewYork,
  [StringValue("New Mexico")]
  NewMexico,
}

您只需使用
myState.GetStringValue()

我看到通常通过在枚举成员上放置
[StringValue(“纽约”)]
属性来处理这个问题。一个快速的谷歌搜索会返回一个非常好的方法

基本上使属性类:

public class StringValueAttribute : Attribute {

    public string StringValue { get; protected set; }

    public StringValueAttribute(string value) {
        this.StringValue = value;
    }
}
以及访问它的扩展方法:

   public static string GetStringValue(this Enum value) {
        // Get the type
        Type type = value.GetType();

        // Get fieldinfo for this type
        FieldInfo fieldInfo = type.GetField(value.ToString());

        // Get the stringvalue attributes
        StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
            typeof(StringValueAttribute), false) as StringValueAttribute[];

        // Return the first if there was a match, or enum value if no match
        return attribs.Length > 0 ? attribs[0].StringValue : value.ToString();
    }
那么您的枚举将如下所示:

public enum EnumState{
  [StringValue("New York")]
  NewYork,
  [StringValue("New Mexico")]
  NewMexico,
}

您只需使用
myState.GetStringValue()

您可以使用以下模式:


它将允许您为枚举中的每个枚举定义自定义字符串名称。

您可以使用以下模式:


它将允许您为枚举中的每个枚举定义自定义字符串名称。

如果(EnumState==EnumState.NewYork)
在setter中设置
值的值是个坏主意。为什么要对字符串调用
.ToString()
?通过将字符串值分配给枚举类型的变量,您打算实现什么?无论您试图做什么,都可能有更好的方法来实现它。
if(EnumState==EnumState.NewYork)
在setter中设置
value
的值是个坏主意。为什么要对字符串调用
.ToString()
?通过将字符串值分配给枚举类型的变量,您打算实现什么?无论您想做什么,可能都有更好的方法。如果您需要精确的字符串模拟,为什么不调用EnumState.NewYork.ToString()?@sll-因为
New York
有一个空格
NewYork
没有。对此更好的改进是使用现有的
DisplayNameAttribute
并对其进行扩展。如果需要精确的字符串模拟,为什么不调用EnumState.NewYork.ToString()?@sll?因为
NewYork
有一个空格
NewYork
没有。对此更好的改进是使用现有的
DisplayNameAttribute
并对其进行扩展。