Exemplo n.º 1
0
        /// <summary>
        /// 获取枚举的显示名称
        /// </summary>
        /// <typeparam name="TEnum">枚举</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns>如果返回-999即失败</returns>
        public static int Value <TEnum>(this TEnum value) where TEnum : struct
        {
            if (!(value is Enum))
            {
                return(-999);
            }
            var values = NonGenericEnums.GetMember(value.GetType(), value);

            return(values.ToInt32());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取枚举的显示名称
        /// </summary>
        /// <typeparam name="TEnum">枚举</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public static OperationType Operation <TEnum>(this TEnum value) where TEnum : struct
        {
            if (!(value is Enum))
            {
                return(OperationType.None);
            }
            var values = NonGenericEnums.GetMember(value.GetType(), value);
            var dis    = values.Attributes.Get(typeof(OperationAttribute)) as OperationAttribute;

            return(dis == null ? OperationType.None : dis.Type);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取枚举的显示名称
        /// </summary>
        /// <typeparam name="TEnum">枚举</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public static string DisplayName <TEnum>(this TEnum value) where TEnum : struct
        {
            if (!(value is  Enum))
            {
                return("");
            }
            var values = NonGenericEnums.GetMember(value.GetType(), value);
            var dis    = values.Attributes.Get(typeof(DisplayNameAttribute)) as DisplayNameAttribute;

            return(dis == null ? "" : dis.DisplayName);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 获取枚举的描述
        /// </summary>
        /// <typeparam name="TEnum">枚举</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public static string Description <TEnum>(this TEnum value) where TEnum : struct
        {
            if (!(value is Enum))
            {
                return("");
            }
            var values = NonGenericEnums.GetMember(value.GetType(), value);
            var des    = values.Attributes.Get(typeof(DescriptionAttribute)) as DescriptionAttribute;

            return(des == null ? "" : des.Description);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 字符串转枚举
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static TEnum StringToEnum <TEnum>(this string value) where TEnum : struct
        {
            if (value.IsEmpty())
            {
                return(default(TEnum));
            }
            var values = NonGenericEnums.GetMember(typeof(TEnum), value.ToCamel());

            if (values == null)
            {
                return(default(TEnum));
            }
            return((TEnum)values.Value);
        }
Exemplo n.º 6
0
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
     if (value == null)
     {
         writer.WriteNull();
     }
     else
     {
         var enumType = value.GetType();
         if (EnumFormat != EnumFormat.DecimalValue)
         {
             var member = NonGenericEnums.GetMember(enumType, value);
             if (member != null)
             {
                 writer.WriteValue(member.AsString(EnumFormat));
                 return;
             }
         }
         writer.WriteRawValue(NonGenericEnums.AsString(enumType, value, EnumFormat.DecimalValue));
     }
 }
Exemplo n.º 7
0
 internal EnumConfigurationNode(string key, Enum value, Type enumType) : base(key, value)
 {
     this.EnumType = enumType;
     this.Value    = NonGenericEnums.GetMember(enumType, value)
                     .Attributes.Get <SelectionOptionAttribute>().SerializeAs;
 }