/*-------------------------------------------------------------------------------------------------------------
* 类名:EnumHelper
* 功能说明:业务逻辑使用的所有枚举
* 创建日期:2010-08-09
* 创建人:赵宏卫
* 修改履历:
* 1-创建 [赵宏卫 2010-08-09]
* 2.添加枚举 EnumIsUs [赵宏卫 2010-08-09]
* ---------------------------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace Sp.Common
{
/// <summary>
/// 名称:EnumHelper
/// 描述:枚举的工具类
/// 功能:
/// 1.将枚举解释为中文(枚举中有“Description”的标签描述)
/// </summary>
/// <history>
/// Create by zhaohwi 2010-10-21 ver 0.1
/// </history>
public static class EnumHelper
{
// maps用于保存每种枚举及其对应的EnumMap对象
private static Dictionary<Type, EnumMap> maps;
// 由于C#中没有static indexer的概念,所以在这里我们用静态方法
/// <summary>
/// 取得枚举值的描述信息
/// </summary>
/// <param name="item">枚举值</param>
/// <returns>枚举的描述信息</returns>
public static string GetString(this Enum item, string language = "zh-CN")
{
if (item == null) return string.Empty;
if (maps == null)
{
maps = new Dictionary<Type, EnumMap>();
}
Type enumType = item.GetType();
EnumMap mapper = null;
if (maps.ContainsKey(enumType))
{
mapper = maps[enumType];
}
else
{
mapper = new EnumMap(enumType);
maps.Add(enumType, mapper);
}
if (!mapper.Exist(item)) return string.Empty;
//本地化 begin
string result = mapper[item].FirstOrDefault(
p => p.Key.ToLower().Trim() == language.Trim().ToLower()).Value;
return result;
//end
//return mapper[item];
}
/// <summary>
/// 取得枚举值的描述信息
/// </summary>
/// <param name="value">枚举值</param>
/// <returns>枚举的描述信息</returns>
public static string GetString(this object value, string language = "zh-CN")
{
string retVal = string.Empty;
try
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
EnumDescriptionAttribute[] attributes =
(EnumDescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
//retVal = ((attributes.Length > 0) ? attributes[0].Description : value.ToString());
if (string.IsNullOrEmpty(language))
{
retVal = attributes[0].Description;// : value.ToString());
}
else
{
if (language.Trim().ToLower() == "zh-CN".ToLower())
{
retVal = attributes[0].Description;
}
else if (language.Trim().ToLower() == "en-US".ToLower())
{
retVal = attributes[0].DescriptionUS;
}
}
}
}
catch (NullReferenceException) { }
finally
{
if (string.IsNullOrEmpty(retVal)) retVal = "";
}
return retVal;
}
/// <summary>
/// 取得枚举值的描述信息
/// </summary>
/// <param name="value">枚举值</param>
/// <returns>枚举的描述信息</returns>
public static string GetDisplayString(this object value)
{
string retVal = string.Empty;
try
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
DisplayNameAttribute[] attributes =
(DisplayNameAttribute[])fieldInfo.GetCustomAttributes(typeof(DisplayNameAttribute), false);
retVal = ((attributes.Length > 0) ? attributes[0].DisplayName : value.ToString());
}
catch (NullReferenceException) { }
finally
{
if (string.IsNullOrEmpty(retVal)) retVal = "";
}
return retVal;
}
/// <summary>
/// 取得枚举值的描述信息
/// </summary>
/// <param name="value">枚举值</param>
/// <returns>枚举的描述信息</returns>
public static string GetDescriptionString(this object value, string language = "zh-CN")
{
string retVal = string.Empty;
try
{
retVal = GetString(value, language);
}
catch (NullReferenceException) { }
finally
{
if (string.IsNullOrEmpty(retVal)) retVal = "";
}
return retVal;
}
}
/// <summary>
/// 名称:EnumMap
/// 描述:枚举的值与描述的映射
/// 功能:
/// 1.枚举的值与描述的映射
/// </summary>
/// <history>
/// Create by zhaohwi 2010-10-21 ver 0.1
/// </history>
public class EnumMap
{
private Type internalEnumType = null;
private Dictionary<Enum, IList<KeyValuePair<string, string>>> map = null;
private Dictionary<int, IList<KeyValuePair<string, string>>> valueMap = null;
private Dictionary<int, string> displayMap = null;
/// <summary>
/// 是否包含指定的枚举项
/// </summary>
/// <param name="item">要查找的枚举项</param>
/// <returns>是否存在</returns>
public bool Exist(Enum item)
{
return map.ContainsKey(item);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="enumType">枚举的类型</param>
public EnumMap(Type enumType)
{
if (!enumType.IsSubclassOf(typeof(Enum)))
{
throw new InvalidCastException();
}
internalEnumType = enumType;
FieldInfo[] staticFiles = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
map = new Dictionary<Enum, IList<KeyValuePair<string, string>>>(staticFiles.Length);
valueMap = new Dictionary<int, IList<KeyValuePair<string, string>>>(staticFiles.Length);
displayMap = new Dictionary<int, string>(staticFiles.Length);
for (int i = 0; i < staticFiles.Length; i++)
{
if (staticFiles[i].FieldType == enumType)
{
string description = "";
string descriptionUS = "";
string displayName = "";
object[] attrs = staticFiles[i].GetCustomAttributes(typeof(EnumDescriptionAttribute), true);
description = attrs != null && attrs.Length > 0 ?
((EnumDescriptionAttribute)attrs[0]).Description :
//若没找到EnumItemDescription标记,则使用该枚举值的名字
description = staticFiles[i].Name;
descriptionUS = attrs != null && attrs.Length > 0 ?
((EnumDescriptionAttribute)attrs[0]).DescriptionUS :
//若没找到EnumItemDescription标记,则使用该枚举值的名字
description = staticFiles[i].Name;
//本地化 begin
KeyValuePair<string, string> cn = new KeyValuePair<string, string>("zh-CN", description);
KeyValuePair<string, string> en = new KeyValuePair<string, string>("en-US", descriptionUS);
IList<KeyValuePair<string, string>> descList = new List<KeyValuePair<string, string>>();
descList.Add(cn);
descList.Add(en);
//end
object[] dispAttrs = staticFiles[i].GetCustomAttributes(typeof(DisplayNameAttribute), true);
displayName = dispAttrs != null && dispAttrs.Length > 0 ?
((EnumDescriptionAttribute)dispAttrs[0]).Description :
//若没找到EnumItemDescription标记,则使用该枚举值的名字
displayName = staticFiles[i].Name;
//map.Add((Enum)staticFiles[i].GetValue(enumType), description);
map.Add((Enum)staticFiles[i].GetValue(enumType), descList);
valueMap.Add((int)staticFiles[i].GetValue(enumType), descList);
//valueMap.Add((int)staticFiles[i].GetValue(enumType), description);
displayMap.Add((int)staticFiles[i].GetValue(enumType), displayName);
}
}
}
/// <summary>
/// 根据枚举取得字典信息(枚举,枚举描述)
/// </summary>
public Dictionary<Enum, IList<KeyValuePair<string, string>>> EnumMaps
{
get { return map; }
}
/// <summary>
/// 根据枚举取得字典信息(int,枚举描述)
/// </summary>
public Dictionary<int, IList<KeyValuePair<string, string>>> ValueMaps
{
get { return valueMap; }
}
public Dictionary<int, string> DisplayMaps
{
get { return displayMap; }
}
/// <summary>
/// 枚举的索引方法
/// </summary>
/// <param name="item">枚举值</param>
/// <returns>枚举的描述信息</returns>
public IList<KeyValuePair<string, string>> this[Enum item]
{
get
{
if (item.GetType() != internalEnumType)
{
throw new ArgumentException();
}
return map[item];
}
}
/// <summary>
///
/// </summary>
/// <param name="language"></param>
/// <returns></returns>
public Dictionary<int, string> GetValueMaps(string language = "zh-CN")
{
Dictionary<int, string> result = null;
string tmpLanguage = string.IsNullOrEmpty(language) ? "zh-CN" : language;
foreach (KeyValuePair<int, IList<KeyValuePair<string, string>>> obj
in ValueMaps)
{
if (result == null) result = new Dictionary<int, string>();
KeyValuePair<string, string> defaultNameObject =
obj.Value.FirstOrDefault(
p => p.Key.ToLower().Trim() == tmpLanguage.Trim().ToLower());
string name = string.Empty;
if (!string.IsNullOrEmpty(defaultNameObject.Key))
{
name = defaultNameObject.Value;
result.Add(obj.Key, name);
}
}
return result;
}
}
}