枚举与下拉列表的空间属性 [英] enum with space property for dropdownlist

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

问题描述

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

  public enum EnumState 
{
NewYork,
NewMexico
}


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


解决方案

我已经看到这一般通过在枚举成员上放置一个 [StringValue(New York)] 属性来处理。快速谷歌搜索返回这个博客文章有一个很好的方法。



基本上使属性类:

  public class StringValueAttribute:Attribute {

public string StringValue {get;保护套}

public StringValueAttribute(string value){
this.StringValue = value;
}
}

还有一种扩展方法来访问它:

  public static string GetStringValue(此枚举值){
//获取类型
类型type = value.GetType ();

//获取此类型的fieldinfo
FieldInfo fieldInfo = type.GetField(value.ToString());

//获取stringvalue属性
StringValueAttribute []; attribs = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute),false)as StringValueAttribute [];

//如果有匹配,返回第一个,如果没有匹配则返回枚举值
返回attribs.Length> 0? attribs [0] .StringValue:value.ToString();
}

那么你的枚举将如下所示:

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

,你可以使用 myState.GetStringValue();


since i can not have space in enum and i am trying to do something like this but seems like does not like it....

public enum EnumState
{
    NewYork,
    NewMexico
}


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

解决方案

I have seen this generally handled by putting a [StringValue("New York")] attribute on the enum members. A quick google search returns this blog post, which has a pretty good way of doing it.

Basically make the attribute class:

public class StringValueAttribute : Attribute {

    public string StringValue { get; protected set; }

    public StringValueAttribute(string value) {
        this.StringValue = value;
    }
}

And an extension method to access it:

   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();
    }

Then your enum would look like this:

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

and you can just use myState.GetStringValue();

这篇关于枚举与下拉列表的空间属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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