using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Pcb.Common.Attributes;
namespace Pcb.Common
{
///
/// 枚举帮助类
///
/// @2015.6.18
///
public static class EnumHelper
{
///
/// 转成列表
///
///
///
public static List ToList()
{
var fields = typeof(T).GetFields();
var list = (from f in fields
where f.FieldType == typeof(T)
select (T)f.GetRawConstantValue()
).ToList();
return list;
}
///
/// 返回枚举值对应的NoteAttribute注释信息
///
/// 枚举
/// 值
/// 注释
public static string GetNoteByVal(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;
}
///
/// 返回常量对应的NoteAttribute注释信息
///
/// 常量类
/// 值
/// 注释
public static string GetNoteByConst(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;
}
///
/// 返回常量对应的NoteAttribute注释信息
///
/// 常量类
/// 值
/// 注释
public static string GetCacheTypeNote(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(CacheKeyNoteAttribute)) as CacheKeyNoteAttribute;
return attr == null ? string.Empty : attr.Note;
}
///
/// 枚举值转int16
///
/// 枚举
/// int16(short)
public static short ToShort(this System.Enum source)
{
return Convert.ToInt16(source.GetHashCode());
}
///
/// 枚举值转byte
///
/// 枚举
/// byte
public static byte ToByte(this System.Enum source)
{
return Convert.ToByte(source.GetHashCode());
}
///
/// 返回枚举注释
///
/// 枚举
/// 注释
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 GetNotes() where TEnum : struct
{
var notes = new Dictionary();
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);
}
///
/// 获取常量的属性
///
///
///
///
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;
}
///
/// 将枚举转换成下拉列表
///
///
///
public static List ToSelectListItem()
{
List listStatus = new List();
try
{
var fields = typeof(T).GetFields();
foreach (var f in fields)
{
var attr = f.CustomAttributes.Where(x => x.AttributeType.FullName == typeof(NoteAttribute).FullName).FirstOrDefault();
if (attr != null)
{
listStatus.Add(new SelectListItem() { Value = f.GetRawConstantValue().ToString(), Text = attr.ConstructorArguments[0].Value.ToString() });
}
}
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
}
return listStatus;
}
public static List ToSelectListItemInHasCode()
{
List listStatus = new List();
try
{
var fields = typeof(T).GetFields();
foreach (var f in fields)
{
var attr = f.CustomAttributes.Where(x => x.AttributeType.FullName == typeof(NoteAttribute).FullName).FirstOrDefault();
if (attr != null)
{
listStatus.Add(new SelectListItem() { Value = f.Name, Text = attr.ConstructorArguments[0].Value.ToString() });
}
}
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
}
return listStatus;
}
#region 戴雁冰扩展
///
/// 获取类型的说明
///
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;
}
///
/// 转成值与说明的字典
///
public static Dictionary _GetValueNoteDicFromEnumType(this Type EnmType)
{
var ns = System.Enum.GetNames(EnmType);
var vs = System.Enum.GetValues(EnmType); ;
var dic = new Dictionary(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);
///
/// 获取类型的说明
///
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;
}
///
/// 获取枚举值的说明
///
public static string _GetDescriptionByEnumValue(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;
}
///
/// 获取枚举值的说明
///
public static string _GetNoteByEnumValue(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;
}
///
/// 转成值与说明的字典
///
public static Dictionary _GetValueDescriptionDicFromEnumType(this Type EnmType)
{
var ns = System.Enum.GetNames(EnmType);
var vs = System.Enum.GetValues(EnmType); ;
var dic = new Dictionary(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);
///
/// 获取中文的说明
///
public static string _GetCnByEnumValue(this TEnum EnmValue)
{
var EnmType = typeof(TEnum);
FieldInfo finfo = EnmType.GetField(EnmValue.ToString());
if (finfo == null) { return string.Empty; }
var enumAttr = finfo.GetCustomAttributes(_CnAttribute, true);
if (enumAttr.Length == 0) { return string.Empty; }
var atr = enumAttr[0] as CnAttribute;
return atr.Remaks ?? string.Empty;
}
///
/// 转成值与中文的说明的字典
///
public static Dictionary _GetValueCnDicFromEnumType(this Type EnmType)
{
var ns = System.Enum.GetNames(EnmType);
var vs = System.Enum.GetValues(EnmType); ;
var dic = new Dictionary(ns.Length);
for (int i = 0; i < ns.Length; i++)
{
FieldInfo finfo = EnmType.GetField(ns[i]);
object[] atro = finfo.GetCustomAttributes(_CnAttribute, true);
string remark = string.Empty;
if (atro.Length > 0)
{
var atr = atro[0] as CnAttribute;
if (!string.IsNullOrEmpty(atr.Remaks))
{
remark = atr.Remaks;
}
}
dic.Add((TNumType)vs.GetValue(i), remark);
}
return dic;
}
static Type _CnAttribute = typeof(CnAttribute);
///
/// 获取中文的说明
///
public static string _GetEnByEnumValue(this TEnum EnmValue)
{
var EnmType = typeof(TEnum);
FieldInfo finfo = EnmType.GetField(EnmValue.ToString());
if (finfo == null) { return string.Empty; }
var atro = finfo.GetCustomAttributes(_EnAttribute, true);
if (atro.Length == 0) { return string.Empty; }
var atr = atro[0] as EnAttribute;
return atr.Remaks ?? string.Empty;
}
///
/// 转成值与中文的说明的字典
///
public static Dictionary _GetValueEnDicFromEnumType(this Type EnmType)
{
var ns = System.Enum.GetNames(EnmType);
var vs = System.Enum.GetValues(EnmType); ;
var dic = new Dictionary(ns.Length);
for (int i = 0; i < ns.Length; i++)
{
FieldInfo finfo = EnmType.GetField(ns[i]);
object[] atro = finfo.GetCustomAttributes(_EnAttribute, true);
string remark = string.Empty;
if (atro.Length > 0)
{
var atr = atro[0] as EnAttribute;
if (!string.IsNullOrEmpty(atr.Remaks))
{
remark = atr.Remaks;
}
}
dic.Add((TNumType)vs.GetValue(i), remark);
}
return dic;
}
static Type _EnAttribute = typeof(EnAttribute);
///
/// 获取枚举
///
public static TEnum _GetEnumByName(this string Name, TEnum DefValue) where TEnum : struct
{
TEnum enm;
return System.Enum.TryParse(Name, true, out enm) ? enm : DefValue;
}
#endregion
}
public class NoteAttribute : Attribute
{
public string Note { get; set; }
public NoteAttribute(string note)
{
this.Note = note;
}
}
}