using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.ComponentModel.DataAnnotations;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace CommonHelper
|
{
|
/// <summary>
|
/// 对应表的字段名称
|
/// </summary>
|
public class EnumColnameAttribute : Attribute
|
{
|
public EnumColnameAttribute(string colname)
|
{
|
ColName = colname;
|
}
|
|
public string ColName { get; set; }
|
}
|
|
/// <summary>
|
/// 保留几位小数
|
/// </summary>
|
public class EnumDecimalDigitsAttribute : Attribute
|
{
|
public EnumDecimalDigitsAttribute(int digits)
|
{
|
Digits = digits;
|
}
|
|
public int Digits;
|
}
|
|
/// <summary>
|
/// 枚举类型操作公共类。
|
/// </summary>
|
public static class EnumHelper
|
{
|
/// <summary>
|
/// 获取枚举所有成员名称。
|
/// </summary>
|
/// <typeparam name="T">枚举类型</typeparam>
|
public static string[] GetNames<T>()
|
{
|
return Enum.GetNames(typeof(T));
|
}
|
|
/// <summary>
|
/// 检测枚举是否包含指定成员。
|
/// </summary>
|
/// <typeparam name="T">枚举类型</typeparam>
|
/// <param name="member">成员名或成员值</param>
|
public static bool IsDefined(this Enum value)
|
{
|
Type type = value.GetType();
|
return Enum.IsDefined(type, value);
|
}
|
|
/// <summary>
|
/// 返回指定枚举类型的指定值的描述。
|
/// </summary>
|
/// <param name="t">枚举类型</param>
|
/// <param name="v">枚举值</param>
|
/// <returns></returns>
|
public static string GetDescription(this Enum value)
|
{
|
try
|
{
|
Type type = value.GetType();
|
FieldInfo field = type.GetField(value.ToString());
|
DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
return (attributes.Length > 0) ? attributes[0].Description : string.Empty;
|
}
|
catch
|
{
|
return string.Empty;
|
}
|
}
|
|
public static string GetDisplayName(this Enum eum)
|
{
|
var type = eum.GetType();//先获取这个枚举的类型
|
var field = type.GetField(eum.ToString());//通过这个类型获取到值
|
var obj = (DisplayAttribute)field.GetCustomAttribute(typeof(DisplayAttribute));//得到特性
|
return obj.Name ?? "";
|
}
|
|
/// <summary>
|
/// 返回指定枚举类型的指定值 对应的列名。
|
/// </summary>
|
/// <param name="t"></param>
|
/// <returns></returns>
|
public static string GetColName(this Enum t)
|
{
|
var t_type = t.GetType();
|
var fieldName = Enum.GetName(t_type, t);
|
var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
|
var enumDisplayAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumColnameAttribute))) as EnumColnameAttribute;
|
return enumDisplayAttribute == null ? fieldName : enumDisplayAttribute.ColName;
|
}
|
|
/// <summary>
|
/// 返回指定枚举类型的指定值 对应的列名。
|
/// </summary>
|
/// <param name="t"></param>
|
/// <returns></returns>
|
public static int GetDecimalDigits(this Enum t)
|
{
|
var t_type = t.GetType();
|
var fieldName = Enum.GetName(t_type, t);
|
var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
|
var enumDisplayAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumDecimalDigitsAttribute))) as EnumDecimalDigitsAttribute;
|
return enumDisplayAttribute?.Digits ?? 0;
|
}
|
|
/// <summary>
|
/// 将枚举转换成字典集合
|
/// </summary>
|
/// <typeparam name="T">枚举类型</typeparam>
|
/// <returns></returns>
|
public static Dictionary<int,string> Enum2Dict<T>()
|
{
|
|
Dictionary<int, string> resultList = new Dictionary<int, string>();
|
Type type = typeof(T);
|
var strList = GetNames<T>().ToList();
|
foreach (string key in strList)
|
{
|
string val = Enum.Format(type, Enum.Parse(type, key), "d");
|
resultList.Add( int.Parse(val), key);
|
}
|
return resultList;
|
}
|
|
/// <summary>
|
/// 将枚举转换成字典集合
|
/// </summary>
|
/// <typeparam name="T">枚举类型</typeparam>
|
/// <returns></returns>
|
public static Dictionary<int, string> EnumDescription2Dict<T>()
|
{
|
|
Dictionary<int, string> resultList = new Dictionary<int, string>();
|
Type type = typeof(T);
|
var strList = GetNames<T>().ToList();
|
foreach (string key in strList)
|
{
|
var e = (Enum)Enum.Parse(type, key);
|
string val = Enum.Format(type, Enum.Parse(type, key), "d");
|
resultList.Add(int.Parse(val), e.GetDescription());
|
}
|
return resultList;
|
}
|
}
|
}
|