示例#1
0
        /// <summary>
        /// 获取枚举内容项,并以KeyValuePair列表的方式返回;其中,Key=枚举的值,Value=枚举的描述
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="appendType">选择是否在列表项前插入一条选项的选项类型,比如是否插入所有|请选择等Key为Null的选项</param>
        /// <param name="customApplyDesc">如果要在列表项前插入一条选项,该选项的自定义描述</param>
        /// <returns></returns>
        public static List <KeyValuePair <Nullable <TEnum>, string> > GetKeyValuePairs <TEnum>(EnumAppendItemType appendType, params string[] customApplyDesc) where TEnum : struct
        {
            List <KeyValuePair <Nullable <TEnum>, string> > keyValuePairList = new List <KeyValuePair <Nullable <TEnum>, string> >();
            Type enumType = typeof(TEnum);

            if (enumType.IsEnum || IsGenericEnum(enumType))
            {
                Dictionary <TEnum, string> dic = EnumHelper.GetDescriptions <TEnum>();
                if (dic != null && dic.Count > 0)
                {
                    foreach (TEnum e in dic.Keys)
                    {
                        keyValuePairList.Add(new KeyValuePair <Nullable <TEnum>, string>(e, dic[e]));
                    }
                }
                if (appendType != EnumAppendItemType.None)
                {
                    if (customApplyDesc != null && customApplyDesc.Length > 0 && !string.IsNullOrEmpty(customApplyDesc[0]))
                    {
                        KeyValuePair <Nullable <TEnum>, string> kv = new KeyValuePair <Nullable <TEnum>, string>(null, customApplyDesc[0]);
                        keyValuePairList.Insert(0, kv);
                    }
                    else
                    {
                        string desc = GetEnumDescription(appendType);
                        if (!string.IsNullOrEmpty(desc))
                        {
                            KeyValuePair <Nullable <TEnum>, string> kv = new KeyValuePair <Nullable <TEnum>, string>(null, desc);
                            keyValuePairList.Insert(0, kv);
                        }
                    }
                }
            }

            return(keyValuePairList);
        }
示例#2
0
        /// <summary>
        /// 将枚举值转换成KeyValuePair&lt;Nullable&lt;TEnum>, string&gt;列表返回,
        /// value是通过枚举类型的DescriptionAttribute指定资源类名(如果没有通过DescriptionAttribute指定资源,则默认以"ECCentral.BizEntity.Resources.ResCommonEnum"为资源类),
        /// 从而取得每个枚举值(在资源中以"[枚举类型]_[枚举值字段名称]"为Key)的描述;
        /// </summary>>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="appendType">是否插入“所有”项,且插入到第一个位置,“所有”项的Key值为null。</param>
        /// <returns></returns>
        public static List <KeyValuePair <Nullable <TEnum>, string> > GetKeyValuePairs <TEnum>(EnumAppendItemType appendType) where TEnum : struct
        {
            List <KeyValuePair <Nullable <TEnum>, string> > keyValuePairList = new List <KeyValuePair <Nullable <TEnum>, string> >();
            Type enumType = typeof(TEnum);

            if (enumType.IsEnum)
            {
                FieldInfo[] fields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
                global::System.Resources.ResourceManager resourceManager = GetResourceManager(enumType);
                string description = null;
                foreach (FieldInfo field in fields)
                {
                    description = null;
                    string resKey = string.Format("{0}_{1}", enumType.Name, field.Name);
                    description = GetResourceString(resourceManager, resKey);
                    //if (description == null)
                    //{
                    //    object[] attrs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    //    if (attrs != null && attrs.Length > 0)
                    //    {
                    //        DescriptionAttribute desc = attrs[0] as DescriptionAttribute;
                    //        description = desc.Description;
                    //    }
                    //}
                    description = description ?? field.Name;
                    object   enumValue     = field.GetValue(null);
                    object[] ObsoleteAttrs = field.GetCustomAttributes(typeof(ObsoleteAttribute), true);
                    if (ObsoleteAttrs.Length <= 0)
                    {
                        keyValuePairList.Add(new KeyValuePair <Nullable <TEnum>, string>((TEnum)enumValue, description));
                    }
                }
                if (appendType != EnumAppendItemType.None)
                {
                    description = null;
                    string key_CustomSelect = String.Format("{0}__Select", enumType.Name);
                    switch (appendType)
                    {
                    case EnumAppendItemType.All:
                        description = ResCommonEnum.Enum_All;
                        break;

                    case EnumAppendItemType.Select:
                        description = ResCommonEnum.Enum_Select;
                        break;

                    case EnumAppendItemType.Custom_All:
                        description = GetResourceString(resourceManager, enumType.Name) ?? ResCommonEnum.Enum_All;
                        break;

                    case EnumAppendItemType.Custom_Select:
                        description = GetResourceString(resourceManager, key_CustomSelect) ?? ResCommonEnum.Enum_Select;
                        break;
                    }
                    keyValuePairList.Insert(0, new KeyValuePair <Nullable <TEnum>, string>(null, description));
                }
            }
            return(keyValuePairList);
        }