Esempio n. 1
0
        /// <summary>
        /// 解析一种枚举的一个选项
        /// </summary>
        /// <typeparam name="E">枚举类型</typeparam>
        /// <param name="item">需要解析的选项</param>
        /// <returns>解析结果</returns>
        public static EnumInfo AnalysisItem <E>(E item)
        {
            Type type = typeof(E);

            if (!type.IsEnum)
            {
                return(null);
            }
            string item_name = item.ToString();

            foreach (int ival in Enum.GetValues(type))
            {
                string name = Enum.GetName(type, ival);
                if (item_name != name)
                {
                    continue;
                }
                FieldInfo        info = type.GetField(name);
                ExplainAttribute exp  = ExplainAttribute.Extract(info);
                return(new EnumInfo()
                {
                    Name = name,
                    IntValue = ival,
                    Explain = exp.Text,
                });
            }
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// 获得解释特性信息
        /// </summary>
        public static ExplainAttribute Extract(MemberInfo memberInfo)
        {
            ExplainAttribute explainAttr = ReflexHelp.AttributeFindOnly <ExplainAttribute>(memberInfo);

            if (CheckData.IsObjectNull(explainAttr))
            {
                explainAttr = new ExplainAttribute(ERROR_EXPLAIN_TEXT);
            }
            return(explainAttr);
        }
Esempio n. 3
0
        /// <summary>
        /// 解析一种枚举的所有选项
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <returns>解析结果</returns>
        public static EnumInfo[] AnalysisList(Type type)
        {
            if (!type.IsEnum)
            {
                return(new EnumInfo[] { });
            }
            List <EnumInfo> list = new List <EnumInfo>();

            foreach (int ival in Enum.GetValues(type))
            {
                string           name = Enum.GetName(type, ival);
                FieldInfo        info = type.GetField(name);
                ExplainAttribute exp  = ExplainAttribute.Extract(info);
                list.Add(new EnumInfo()
                {
                    Name     = name,
                    IntValue = ival,
                    Explain  = exp.Text,
                });
            }
            return(list.ToArray());
        }