码迷,mamicode.com
首页 > 其他好文 > 详细

获取枚举的title

时间:2015-07-14 17:01:20      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

 public class StringValue : System.Attribute
    {
        private readonly string _value;

        public StringValue(string value)
        {
            _value = value;
        }

        public string Value
        {
            get { return _value; }
        } 
    }
 public static class StringEnum
    {
        public static string GetStringValue(Enum value)
        {
            string output = null;
            Type type = value.GetType();
            FieldInfo fi = type.GetField(value.ToString());
            StringValue[] attrs =
                fi.GetCustomAttributes(typeof (StringValue),
                    false) as StringValue[];
            if (attrs != null && attrs.Length > 0)
            {
                output = attrs[0].Value;
            }
            return output;
        }
    } 

 

private enum SignMagnitude
{
  [StringValue("Negative")]
  Negative = -1,
}

使用方法:

StringEnum.GetStringValue(SignMagnitude.Negative);

获取枚举的title

标签:

原文地址:http://www.cnblogs.com/objectboy/p/4645619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!