namespace NT.Core
{
    /// <summary>
    /// 枚举信息
    /// </summary>
    public struct EnumInfos
    {
        /// <summary>
        /// 枚举字段索引
        /// </summary>
        public int Index { get; set; }
        /// <summary>
        /// 枚举名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        ///枚举值
        /// </summary>
        public int Value { get; set; }
        /// <summary>
        /// 枚举特性信息
        /// </summary>
        public EnumAttribute EnumAttributeInfo { get; set; }
        /// <summary>
        /// 本地化字符串
        /// </summary>
        public string LocalizedString { get; set; }
    }
}

using System.ComponentModel;

namespace NT.Core
{
    /// <summary>
    /// 枚举特性
    /// </summary>
    public sealed class EnumAttribute : DescriptionAttribute
    {
        /// <summary>
        /// 资源名称
        /// </summary>
        public string ResourceName { get; set; }
        /// <summary>
        /// 自定义标识
        /// </summary>
        public string Flag { get; set; }
        /// <summary>
        ///  枚举项描述信息
        /// </summary>
        public new string Description { get; set; }
    }
}
 
#if 枚举格式
public enum Bug : int
{
    /// <summary>
    /// Index=0
    /// Name="Keywords"
    /// Value=11
    /// TranslatedResource="关键字"
    /// </summary>
    [EnumAttribute(ResourceName = "Bug1_Keywords", Description = "描述", Flag = "标识位")]
    Keywords = 11,
    /// <summary>
    /// 大小写错误
    /// </summary>
    [EnumAttribute(ResourceName="Bug1_Csletter")]
    Csletter = 12,
    /// <summary>
    /// 网络错误
    /// </summary>
    [EnumAttribute(ResourceName = "Bug2_NetError")]
    NetError = 21,
    /// <summary>
    /// 连接超时
    /// </summary>
    [EnumAttribute(ResourceName = "Bug2_Timeout")]
    Timeout = 22,
    /// <summary>
    /// 访问受限
    /// </summary>
    [EnumAttribute(ResourceName = "Bug3_Limited")]
    Limited = 31,
}
#endif

using System;
using System.Reflection;
using System.Collections.Generic;
 
 
namespace NT.Core
{
    /// <summary>
    /// 枚举适配器,用于枚举对象绑定,本地化翻译等工作
    /// </summary>
    public class EnumAdapter<EnumType>
    {
        /// <summary>
        ///获取枚举名称集合
        /// </summary>
        public string[] Names
        {
            get { return System.Enum.GetNames(typeof(EnumType)); }
        }
 
        /// <summary>
        /// 获取枚举信息(包括字段的索引、名称、值、特性信息、本地化字符串)
        /// _如果未找到对应的本地化字符串,则显示原字符串
        /// </summary>
        /// <param name="GetLocalizedStrByResourceName">
        /// 实现了从字典表获取本地化字符信息的委托                                            
        /// e.g: StringResource.ResourceManager.GetString(resourceName) 
        /// </param>
        /// <returns></returns>
        public IList<EnumInfos> GetEnumInfos(Func<string, string> GetLocalizedStrByResourceName = null)
        {
            if (typeof(EnumType).IsEnum)
            {
                List<EnumInfos> result = new List<EnumInfos>();
                string[] names = System.Enum.GetNames(typeof(EnumType));
                try
                {
                    int index = 0;
                    foreach (var name in names)
                    {
                        int value = (int)System.Enum.Parse(typeof(EnumType), name, true);
                        FieldInfo info = typeof(EnumType).GetField(name);
                        EnumAttribute[] attreibutes = info.GetCustomAttributes(typeof(EnumAttribute), false) as EnumAttribute[];
                        string localizedString = string.Empty;
                        EnumAttribute enumAttributeInfo = null;
                        if (attreibutes.Length > 0)
                        {
                            //sealed保证了attreibutes的长度为0或1
                            string resourceName = string.IsNullOrEmpty(attreibutes[0].ResourceName) ?
                                name : attreibutes[0].ResourceName;
                            string localizedStr = GetLocalizedStrByResourceName != null ?
                                GetLocalizedStrByResourceName(resourceName) : string.Empty;
                            localizedString = string.IsNullOrEmpty(localizedStr) ?
                                resourceName : localizedStr;
                            string description = string.IsNullOrEmpty(attreibutes[0].Description) ?
                                string.Empty : attreibutes[0].Description;
                            string flag = string.IsNullOrEmpty(attreibutes[0].Flag) ?
                                string.Empty : attreibutes[0].Flag;
                            enumAttributeInfo = new EnumAttribute
                            {
                                ResourceName = resourceName,
                                Description = description,
                                Flag = flag
                            };
                        }
                        else
                        {
                            string localizedStr = GetLocalizedStrByResourceName != null ?
                                GetLocalizedStrByResourceName(name) : string.Empty;
                            localizedString = string.IsNullOrEmpty(localizedStr) ? name : localizedStr;
                        }
                        var infos = new EnumInfos()
                        {
                            Index = index++,
                            Name = name,
                            Value = value,
                            EnumAttributeInfo = enumAttributeInfo,
                            LocalizedString = localizedString
                        };
                        result.Add(infos);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return result;
            }
            else
            {
                throw new Exception("Type error, please make sure your type is enum!");
            }
        }
    }
}
posted on 2014-02-20 18:21  布史  阅读(708)  评论(0编辑  收藏  举报