//----------------------------------------------------------------
//Module Name: EnumHelper
//Purpose:
//CopyRight: Muchinfo
//History:
//----------------------------------------------------------------
//DateTime 2015/1/9 9:48:18
//Author ouyang.hongbin
//Description Create
//----------------------------------------------------------------
using Muchinfo.MTPClient.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Muchinfo.MTPClient.Data.Helper
{
///
/// DropDataTemplate,Enum转List Model
///
public class DropDataTemplate
{
private int _Index;
///
/// 排序
///
public int Index
{
get { return _Index; }
set { _Index = value; }
}
private string _Description;
///
/// 说明
///
public string Description
{
get { return _Description; }
set { _Description = value; }
}
private int _Value;
///
/// 值
///
public int Value
{
get { return _Value; }
set { _Value = value; }
}
private string _GroupKey;
///
/// 分组关键字
///
public string GroupKey
{
get { return _GroupKey; }
set { _GroupKey = value; }
}
private object _Tag;
///
/// 原先目标
///
public object Tag
{
get { return _Tag; }
set { _Tag = value; }
}
}
[AttributeUsage(AttributeTargets.Field)]
public class KindDiscAttribute : Attribute
{
private string _Description;
///
/// Description
///
public string Description
{
get { return _Description; }
set { _Description = value; }
}
private int _Kind;
///
/// kind
///
public int Kind
{
get { return _Kind; }
set
{
_Kind = Kind;
}
}
///
/// ItemDiscAttribute,kind所属分类
///
/// 说明
/// 所属分类
public KindDiscAttribute(string description, int kind)
{
_Description = description;
_Kind = kind;
}
///
/// ItemDiscAttribute
///
///
public KindDiscAttribute(string description)
{
_Description = description;
}
}
[AttributeUsage(AttributeTargets.Field)]
public class ItemDiscAttribute : Attribute
{
private string _Description;
///
/// Description
///
public string Description
{
get { return _Description; }
set { _Description = Description; }
}
private readonly string _Group;
///
/// getGroup
///
public string[] Group
{
get { return _Group.Split(','); }
}
///
/// ItemDiscAttribute,带分组的
///
/// 说明
/// 分组,如一个Enum同时具有多个分组,请用“,”号隔开
public ItemDiscAttribute(string description, string group)
{
_Description = description;
_Group = group;
}
///
/// ItemDiscAttribute
///
///
public ItemDiscAttribute(string Description)
{
_Description = Description;
}
}
///
/// EnumHelper,枚举工具类
///
public static class EnumHelper
{
private static Dictionary dictDiscs = new Dictionary();
private static Dictionary> _EnumList = new Dictionary>(); //枚举缓存池
///
/// 获取枚举说明,
/// Sim=》TradeDirection.Register.Discription();
///
/// My enum.
/// 数据库枚举实际定义的代码
/// System.String.
public static string Discription(this Enum myEnum, string enumCode="")
{
string strDisc = string.Empty;
//if (dictDiscs.ContainsKey(myEnum))
//{
// strDisc = dictDiscs[myEnum];
//}
//else
//{
strDisc = GetDiscription(myEnum, enumCode);
// dictDiscs.Add(myEnum, strDisc);
// }
return strDisc;
}
///
/// Gets the discription.
///
/// My enum.
/// 数据库枚举实际定义的代码
/// System.String.
private static string GetDiscription(Enum myEnum, string enumCode = "")
{
var enumType = myEnum.GetType();
var val = (int)Enum.Parse(enumType, myEnum.ToString());
//枚举代码不为空,则根据枚举代码+值从常量缓存获取, 否则根据枚举名称+值从常量数据取
var resxKey = string.Format("{0}_{1}", string.IsNullOrWhiteSpace(enumCode) ? enumType.Name: enumCode, val);
if (ConstResxManager.ConstResx != null &&
ConstResxManager.ConstResx.ResourceHashtable != null &&
ConstResxManager.ConstResx.ResourceHashtable.ContainsKey(resxKey))
{
return ConstResxManager.ConstResx.ResourceHashtable[resxKey] as string;
}
FieldInfo fieldInfo = enumType.GetField(myEnum.ToString());
if (fieldInfo == null)
{
return myEnum.ToString();
}
//获取字典的ItemDisc属性
object[] attrs = fieldInfo.GetCustomAttributes(typeof(ItemDiscAttribute), true);
ItemDiscAttribute desc = null;
if (attrs != null && attrs.Length > 0)
{
desc = attrs[0] as ItemDiscAttribute;
}
//若ItemDisc为空,则根据枚举的字符从资源里查找对应描述
string temp = Client_Resource.ResourceManager.GetString(desc == null ? myEnum.ToString() : desc.Description, Client_Resource.Culture);
if (!string.IsNullOrWhiteSpace(temp)) return temp;
return myEnum.ToString();
}
///
///显示 枚举内容
///
/// 属性类型
///
///
public static string Discription(this Enum myEnum)
{
FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
object[] attrs = fieldInfo.GetCustomAttributes(typeof(T), true);
if (attrs != null && attrs.Length > 0)
{
var attributesType = attrs[0].GetType();
var property = attributesType.GetProperty("Description");
return (string)property.GetValue(attrs[0], null);
}
return myEnum.ToString();
}
public static int GetKind(Enum myEnum)
{
FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
if (fieldInfo != null)
{
object[] attrs = fieldInfo.GetCustomAttributes(typeof(KindDiscAttribute), true);
if (attrs != null && attrs.Length > 0)
{
KindDiscAttribute kind = attrs[0] as KindDiscAttribute;
if (kind != null)
{
return kind.Kind;
}
}
}
return 0;
}
///
/// 将枚举转换成 List DropDataTemplate
///
/// 枚举类型
///
public static List EnumToDictionary(Type enumType)
{
string keyName = enumType.FullName;
if (!_EnumList.ContainsKey(keyName))
{
var list = new List();
int index = 0;
foreach (int i in Enum.GetValues(enumType))
{
string name = Enum.GetName(enumType, i);
//取显示名称
string description = string.Empty;
object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
if (atts.Length > 0)
{
description = ((ItemDiscAttribute)atts[0]).Description;
}
list.Add(new DropDataTemplate()
{
Index = index++,
Value = i,
Description = string.IsNullOrEmpty(description) ? name : description,
Tag = Enum.Parse(enumType, name),
});
}
object syncObj = new object();
if (!_EnumList.ContainsKey(keyName))
{
lock (syncObj)
{
if (!_EnumList.ContainsKey(keyName))
{
_EnumList.Add(keyName, list);
}
}
}
}
return _EnumList[keyName];
}
///
/// 将枚举转换成 List DropDataTemplate
///
/// 枚举类型
/// 分组名称
///
public static List EnumToDictionary(Type enumType, string groupName)
{
string keyName = enumType.FullName;
if (!_EnumList.ContainsKey(keyName))
{
var list = new List();
int index = 0;
foreach (int i in Enum.GetValues(enumType))
{
string name = Enum.GetName(enumType, i);
//取显示名称
string description = string.Empty;
string group = string.Empty;
object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
if (!string.IsNullOrWhiteSpace(groupName) && ((ItemDiscAttribute)atts[0]).Group.Where(x => x == groupName).Count() > 0)
{
if (atts.Length > 0)
{
description = ((ItemDiscAttribute)atts[0]).Description;
}
list.Add(new DropDataTemplate()
{
Index = index++,
Value = i,
GroupKey = groupName,
Description = string.IsNullOrEmpty(description) ? name : description,
Tag = Enum.Parse(enumType, name),
});
}
}
object syncObj = new object();
if (!_EnumList.ContainsKey(keyName))
{
lock (syncObj)
{
if (!_EnumList.ContainsKey(keyName))
{
_EnumList.Add(keyName, list);
}
}
}
}
return _EnumList[keyName];
}
///
/// 将枚举转换成 Dictionary
/// 枚举类型
///
public static Dictionary