最近做了一个国外的项目,最后要本地化为西班牙文。在项目开发中遇到各种的枚举操作,如绑定下拉框,传值,枚举名称与值的转换,本地化操作,
枚举项过滤==,于是下决心帮团队革了这个命,写了如下万能代码,名曰枚举适配器,各种操作一步到位。。。
暂且只记下代码,没有本着分享的目的,勿怪!
/************************************************************************* *GUID:16dd25b6-22cd-4184-9ef7-652a3f341ca9 *CLR:4.0.30319.17929 *Machine: USER-KVT0SIKF25 *Author:Hollson *Email:Hollson@QQ.com *Time:2014/2/14 15:01:45 *Code Caption: ***************************************************************************/ 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; /************************************************************************* *GUID:16dd25b6-22cd-4184-9ef7-652a3f341ca9 *CLR:4.0.30319.17929 *Machine: USER-KVT0SIKF25 *Author:Hollson *Email:Hollson@QQ.com *Time:2014/2/14 15:01:45 *Code Caption: ***************************************************************************/ 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; /************************************************************************* *GUID:16dd25b6-22cd-4184-9ef7-652a3f341ca9 *CLR:4.0.30319.17929 *Machine: USER-KVT0SIKF25 *Author:Hollson *Email:Hollson@QQ.com *Time:2014/2/14 15:01:45 *Code Caption: ***************************************************************************/ 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!"); } } } }
原文:http://www.cnblogs.com/Hollson/p/3558217.html