using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.IO;
|
using System.Linq;
|
using System.Reflection;
|
using System.Xml;
|
|
namespace Tiger.Model.Extensions
|
{
|
/// <summary>
|
/// 枚举扩展方法
|
/// </summary>
|
internal static class EnumExtension
|
{
|
/// <summary>
|
/// 获得枚举的名称
|
/// </summary>
|
/// <returns>枚举的名称</returns>
|
public static string GetName(this Enum obj)
|
{
|
return Enum.GetName(obj.GetType(), obj);
|
}
|
|
/// <summary>
|
/// 获得枚举的值
|
/// </summary>
|
/// <returns>枚举的值</returns>
|
public static int GetValue(this Enum obj)
|
{
|
return Convert.ToInt32(obj);
|
}
|
|
/// <summary>
|
/// 获得枚举的描述,如果没有定义描述则使用枚举的名称代替
|
/// </summary>
|
/// <returns>枚举的描述</returns>
|
public static string GetDesc(this Enum obj)
|
{
|
string name = Enum.GetName(obj.GetType(), obj);
|
DescriptionAttribute attribute = obj.GetType().GetField(name).GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
|
return attribute?.Description ?? name;
|
}
|
|
|
/// <summary>
|
/// 根据名称或者描述获取枚举对象
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="description"></param>
|
/// <returns></returns>
|
public static T GetEnum<T>(this string description) where T : struct
|
{
|
Type type = typeof(T);
|
foreach (var field in type.GetFields())
|
{
|
if (field.Name == description)
|
{
|
return (T)field.GetValue(null);
|
}
|
|
var attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), true);
|
if (attributes != null && attributes.FirstOrDefault() != null)
|
{
|
if (attributes.First().Description == description)
|
{
|
return (T)field.GetValue(null);
|
}
|
}
|
}
|
|
throw new ArgumentException($"{type.Name}中未能找到名称或者描述[{description}]对应的枚举.", "Description");
|
}
|
|
/// <summary>
|
/// 根据值获取枚举对象
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="value"></param>
|
/// <returns></returns>
|
public static T GetEnum<T>(this int value) where T : struct
|
{
|
Type type = typeof(T);
|
if (type.IsEnum)
|
{
|
return (T)Enum.Parse(type, value.ToString());
|
}
|
|
throw new TypeLoadException($"{type.Name}不是枚举类型");
|
}
|
|
/// <summary>
|
/// 根据值获取枚举描述
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="value"></param>
|
/// <returns></returns>
|
public static string GetEnumDesc<T>(this int value) where T : struct
|
{
|
Type type = typeof(T);
|
if (type.IsEnum)
|
{
|
return GetDesc((Enum)Enum.Parse(type, value.ToString()));
|
}
|
|
throw new TypeLoadException($"{type.Name}不是枚举类型");
|
}
|
|
/// <summary>
|
/// 获取枚举内容列表
|
/// </summary>
|
/// <param name="type">枚举类型typeof(T)</param>
|
/// <returns>返回枚举内容列表</returns>
|
public static List<EnumContent> GetEnumContent(this Type type)
|
{
|
List<EnumContent> result = new List<EnumContent>();
|
FieldInfo[] fields = type.GetFields();
|
foreach (FieldInfo field in fields)
|
{
|
if (field.FieldType.IsEnum)
|
{
|
object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
result.Add(new EnumContent(field.Name, Convert.ToInt32(field.GetValue(null)), attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description));
|
}
|
}
|
|
return result;
|
}
|
}
|
|
/// <summary>
|
/// 枚举内容实体
|
/// </summary>
|
internal readonly struct EnumContent
|
{
|
/// <summary>
|
/// 枚举内容实体
|
/// </summary>
|
/// <param name="name">名称</param>
|
/// <param name="value">值</param>
|
/// <param name="desc">描述</param>
|
public EnumContent(string name, int value, string desc)
|
{
|
Name = name;
|
Value = value;
|
Desc = desc;
|
}
|
/// <summary>
|
/// 名称
|
/// </summary>
|
public string Name { get; }
|
/// <summary>
|
/// 值
|
/// </summary>
|
public int Value { get; }
|
/// <summary>
|
/// 描述
|
/// </summary>
|
public string Desc { get; }
|
}
|
}
|