using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Reflection;
|
|
namespace JiepeiWMS.Common.Helper
|
{
|
/// <summary>
|
/// 枚举帮助类
|
///
|
/// @2015.6.18
|
/// </summary>
|
public static class EnumHelper
|
{
|
/// <summary>
|
/// 转成列表
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <returns></returns>
|
public static List<T> ToList<T>()
|
{
|
var fields = typeof(T).GetFields();
|
var list = (from f in fields
|
where f.FieldType == typeof(T)
|
select (T)f.GetRawConstantValue()
|
).ToList();
|
return list;
|
}
|
|
/// <summary>
|
/// 返回枚举值对应的NoteAttribute注释信息
|
/// </summary>
|
/// <typeparam name="T">枚举</typeparam>
|
/// <param name="val">值</param>
|
/// <returns>注释</returns>
|
public static string GetNoteByVal<T>(object val) where T : struct
|
{
|
var field = typeof(T).GetFields().FirstOrDefault(x => x.FieldType == typeof(T) && x.GetRawConstantValue() == val);
|
if (field == null) return string.Empty;
|
var attr = field.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(NoteAttribute)) as NoteAttribute;
|
return attr == null ? string.Empty : attr.Note;
|
}
|
|
/// <summary>
|
/// 返回常量对应的NoteAttribute注释信息
|
/// </summary>
|
/// <typeparam name="T">常量类</typeparam>
|
/// <param name="val">值</param>
|
/// <returns>注释</returns>
|
public static string GetNoteByConst<T>(string val)
|
{
|
var field = typeof(T).GetFields().FirstOrDefault(x => x.Name == val);
|
if (field == null) return string.Empty;
|
var attr = field.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(NoteAttribute)) as NoteAttribute;
|
return attr == null ? string.Empty : attr.Note;
|
}
|
|
/// <summary>
|
/// 枚举值转int16
|
/// </summary>
|
/// <param name="source">枚举</param>
|
/// <returns>int16(short)</returns>
|
public static short ToShort(this System.Enum source)
|
{
|
return Convert.ToInt16(source.GetHashCode());
|
}
|
|
/// <summary>
|
/// 枚举值转byte
|
/// </summary>
|
/// <param name="source">枚举</param>
|
/// <returns>byte</returns>
|
public static byte ToByte(this System.Enum source)
|
{
|
return Convert.ToByte(source.GetHashCode());
|
}
|
|
/// <summary>
|
/// 返回枚举注释
|
/// </summary>
|
/// <param name="source">枚举</param>
|
/// <returns>注释</returns>
|
public static string ToNote(this System.Enum source)
|
{
|
var field = source.GetType().GetField(source.ToString());
|
if (field == null) { return string.Empty; }
|
var attr = field.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(NoteAttribute)) as NoteAttribute;
|
return attr == null ? string.Empty : attr.Note;
|
}
|
|
public static Dictionary<int, string> GetNotes<TEnum>() where TEnum : struct
|
{
|
var notes = new Dictionary<int, string>();
|
|
var fields = typeof(TEnum).GetFields();
|
foreach (var f in fields)
|
{
|
if (f.FieldType == typeof(TEnum))
|
{
|
var attr = f.GetCustomAttribute(typeof(NoteAttribute)) as NoteAttribute;
|
string note = null;
|
if (attr != null)
|
{
|
note = attr.Note;
|
}
|
|
var rawConstantValue = (int)f.GetRawConstantValue();
|
notes.Add(rawConstantValue, note);
|
}
|
|
}
|
|
return notes;
|
}
|
|
public static string ToNote(this int val, Type T)
|
{
|
Type type = T;
|
string name = System.Enum.GetName(type, val);
|
if (name == null)
|
return string.Empty;
|
FieldInfo fd = type.GetField(name);
|
object[] attrs = fd.GetCustomAttributes(typeof(NoteAttribute), false);
|
string note = string.Empty;
|
foreach (NoteAttribute attr in attrs)
|
{
|
note = attr.Note;
|
}
|
return note;
|
}
|
|
public static string ToNote(this int? val, Type T)
|
{
|
if (val == null) return "";
|
return ToNote(val.Value, T);
|
}
|
|
/// <summary>
|
/// 获取常量的属性
|
/// </summary>
|
/// <param name="val"></param>
|
/// <param name="T"></param>
|
/// <returns></returns>
|
public static string ToConstNote(this string val, Type T)
|
{
|
Type type = T;
|
var info = type.GetFields();
|
if (val == null)
|
return string.Empty;
|
FieldInfo fd = type.GetField(val);
|
object[] attrs = fd.GetCustomAttributes(typeof(NoteAttribute), false);
|
string note = string.Empty;
|
foreach (NoteAttribute attr in attrs)
|
{
|
note = attr.Note;
|
}
|
return note;
|
}
|
|
#region 戴雁冰扩展
|
/// <summary>
|
/// 获取类型的说明
|
/// </summary>
|
public static string _GetNoteAttr(this Type TypeObj)
|
{
|
var enumAttr = TypeObj.GetCustomAttributes(_NoteAttribute, true);
|
if (enumAttr.Length == 0) { return string.Empty; }
|
|
var desc = enumAttr[0] as NoteAttribute;
|
return desc.Note ?? string.Empty;
|
}
|
/// <summary>
|
/// 转成值与说明的字典
|
/// </summary>
|
public static Dictionary<TNumType, string> _GetValueNoteDicFromEnumType<TNumType>(this Type EnmType)
|
{
|
var ns = System.Enum.GetNames(EnmType);
|
var vs = System.Enum.GetValues(EnmType); ;
|
var dic = new Dictionary<TNumType, string>(ns.Length);
|
for (int i = 0; i < ns.Length; i++)
|
{
|
FieldInfo finfo = EnmType.GetField(ns[i]);
|
object[] enumAttr = finfo.GetCustomAttributes(_NoteAttribute, true);
|
string remark = string.Empty;
|
if (enumAttr.Length > 0)
|
{
|
var desc = enumAttr[0] as NoteAttribute;
|
if (!string.IsNullOrEmpty(desc.Note))
|
{
|
remark = desc.Note;
|
}
|
}
|
dic[(TNumType)vs.GetValue(i)] = remark;
|
}
|
return dic;
|
}
|
static Type _NoteAttribute = typeof(NoteAttribute);
|
/// <summary>
|
/// 获取类型的说明
|
/// </summary>
|
public static string _GetDescriptionAttr(this Type TypeObj)
|
{
|
var enumAttr = TypeObj.GetCustomAttributes(_DesciptionAttribute, true);
|
if (enumAttr.Length == 0) { return string.Empty; }
|
|
var desc = enumAttr[0] as DescriptionAttribute;
|
return desc.Description ?? string.Empty;
|
}
|
/// <summary>
|
/// 获取枚举值的说明
|
/// </summary>
|
public static string _GetDescriptionByEnumValue<TEnum>(this TEnum EnmValue)
|
{
|
var EnmType = typeof(TEnum);
|
|
FieldInfo finfo = EnmType.GetField(EnmValue.ToString());
|
if (finfo == null) { return string.Empty; }
|
|
var enumAttr = finfo.GetCustomAttributes(_DesciptionAttribute, true);
|
if (enumAttr.Length == 0) { return string.Empty; }
|
|
var desc = enumAttr[0] as DescriptionAttribute;
|
return desc.Description ?? string.Empty;
|
}
|
/// <summary>
|
/// 获取枚举值的说明
|
/// </summary>
|
public static string _GetNoteByEnumValue<TEnum>(this TEnum EnmValue)
|
{
|
var EnmType = typeof(TEnum);
|
|
FieldInfo finfo = EnmType.GetField(EnmValue.ToString());
|
if (finfo == null) { return string.Empty; }
|
|
var enumAttr = finfo.GetCustomAttributes(_NoteAttribute, true);
|
if (enumAttr.Length == 0) { return string.Empty; }
|
|
var desc = enumAttr[0] as NoteAttribute;
|
return desc.Note ?? string.Empty;
|
}
|
/// <summary>
|
/// 转成值与说明的字典
|
/// </summary>
|
public static Dictionary<TNumType, string> _GetValueDescriptionDicFromEnumType<TNumType>(this Type EnmType)
|
{
|
var ns = System.Enum.GetNames(EnmType);
|
var vs = System.Enum.GetValues(EnmType); ;
|
var dic = new Dictionary<TNumType, string>(ns.Length);
|
for (int i = 0; i < ns.Length; i++)
|
{
|
FieldInfo finfo = EnmType.GetField(ns[i]);
|
object[] enumAttr = finfo.GetCustomAttributes(_DesciptionAttribute, true);
|
string remark = string.Empty;
|
if (enumAttr.Length > 0)
|
{
|
var desc = enumAttr[0] as DescriptionAttribute;
|
if (!string.IsNullOrEmpty(desc.Description))
|
{
|
remark = desc.Description;
|
}
|
}
|
dic.Add((TNumType)vs.GetValue(i), remark);
|
}
|
return dic;
|
}
|
static Type _DesciptionAttribute = typeof(DescriptionAttribute);
|
/// <summary>
|
/// 获取枚举
|
/// </summary>
|
public static TEnum _GetEnumByName<TEnum>(this string Name, TEnum DefValue) where TEnum : struct
|
{
|
TEnum enm;
|
return System.Enum.TryParse<TEnum>(Name, true, out enm) ? enm : DefValue;
|
}
|
/// <summary>
|
/// 转成名称与值的字典
|
/// </summary>
|
public static Dictionary<string, TNumType> _GetNameValueDicFromEnumType<TNumType>(this Type EnmType)
|
{
|
var ns = System.Enum.GetNames(EnmType);
|
var vs = System.Enum.GetValues(EnmType); ;
|
var dic = new Dictionary<string, TNumType>(ns.Length);
|
for (int i = 0; i < ns.Length; i++)
|
{
|
dic.Add(ns[i], (TNumType)vs.GetValue(i));
|
}
|
return dic;
|
}
|
#endregion
|
}
|
|
public class NoteAttribute : Attribute
|
{
|
public string Note { get; set; }
|
|
public NoteAttribute(string note)
|
{
|
this.Note = note;
|
}
|
}
|
}
|