コード例 #1
0
        /// <summary>
        /// GetEnumValueByTag 根据描述Tag获取对应的枚举值
        /// </summary>
        public static object GetEnumValueByTag(Type enumType, object tag)
        {
            IList <EnumDescriptionAttribute> list = EnumDescriptionAttribute.GetDescriptions(enumType);

            if (list == null)
            {
                return(null);
            }
            return(list.ConvertFirst <EnumDescriptionAttribute, object>(ed => ed.enumValue, ed => string.Equals(ed.Tag.ToString(), tag.ToString())));
        }
コード例 #2
0
        /// <summary>
        /// GetFieldTag 获得指定枚举值的Tag。
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static object GetFieldTag(object enumValue)
        {
            IList <EnumDescriptionAttribute> list = EnumDescriptionAttribute.GetDescriptions(enumValue.GetType());

            if (list == null)
            {
                return(null);
            }

            return(list.ConvertFirst <EnumDescriptionAttribute, object>(ed => ed.Tag, ed => string.Equals(ed.EnumValue.ToString(), enumValue.ToString())));
        }
コード例 #3
0
        /// <summary>
        /// GetFieldDescriptionText 获得指定枚举值的描述文本。
        /// </summary>
        public static string GetFieldText(object enumValue)
        {
            IList <EnumDescriptionAttribute> list = EnumDescriptionAttribute.GetDescriptions(enumValue.GetType());

            if (list == null)
            {
                return(null);
            }
            list.ToArray();
            return(list.ConvertFirst <EnumDescriptionAttribute, string>(ed => ed.Description, ed => string.Equals(ed.EnumValue.ToString(), enumValue.ToString())));
        }
コード例 #4
0
        public static T GetEnumValueByDescription <T>(string description)
        {
            IList <EnumDescriptionAttribute> list = EnumDescriptionAttribute.GetDescriptions(typeof(T));

            return((T)list.First(ed => string.Equals(ed.Description, description)).EnumValue);
        }