获取枚举的title

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

posted @ 2015-07-14 15:39  等待是一生最初的苍老  阅读(283)  评论(0编辑  收藏  举报