12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reflection;
- namespace Common
- {
- /// <summary>
- /// 枚举扩展方法
- /// </summary>
- public static class EnumExtension
- {
- /// <summary>
- /// 返回枚举项的描述信息。
- /// </summary>
- /// <param name="value">要获取描述信息的枚举项。</param>
- /// <returns>枚举想的描述信息。</returns>
- public static string GetEnumDescription(this Enum value)
- {
- Type enumType = value.GetType();
- // 获取枚举常数名称。
- string name = Enum.GetName(enumType, value);
- if (name != null)
- {
- // 获取枚举字段。
- FieldInfo fieldInfo = enumType.GetField(name);
- if (fieldInfo != null)
- {
- // 获取描述的属性。
- DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
- typeof(DescriptionAttribute), false) as DescriptionAttribute;
- if (attr != null)
- {
- return attr.Description;
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 获取彩种对应数据库表名称
- /// </summary>
- /// <param name="value">彩种枚举类型</param>
- /// <returns></returns>
- public static string GetLotteryTableName(this Enum value)
- {
- if (value == null)
- {
- throw new ArgumentException("value");
- }
- string tableName = String.Empty;
- var fieldInfo = value.GetType().GetField(value.ToString());
- var attributes = (CustomAttribute.TableNameAttribute[])fieldInfo.GetCustomAttributes(typeof(CustomAttribute.TableNameAttribute), false);
- if (attributes.Length > 0)
- {
- tableName = attributes[0].TableName;
- }
- return tableName;
- }
- }
- }
|