随笔 - 17  文章 - 0  评论 - 13  阅读 - 63004 
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   布史  阅读(716)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
点击右上角即可分享
微信分享提示