using System;
using System.ComponentModel;
namespace Epost.Common
{
public static class EnumHelper
{
#region 枚举描述输出
///
/// 枚举描述输出
///
///
///
public static string EMToDescriptionString(this System.Enum e)
{
Type enumType = e.GetType();
if (!enumType.IsEnum)
{
throw new ArgumentException("enumItem requires a Enum ");
}
string name = System.Enum.GetName(enumType, e);
if (name == null)
return string.Empty;
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return string.Empty;
}
else
{
DescriptionAttribute attr = objs[0] as DescriptionAttribute;
return attr.Description;
}
}
#endregion
#region 枚举值输出描述
///
/// 枚举值输出描述
///
///
///
///
public static string EMToEnumDescriptionString(this Nullable i)
{
if (!(i > 0))
{
return string.Empty;
}
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
throw new ArgumentException("enumItem requires a Enum ");
}
string name = System.Enum.GetName(enumType, i);
if (name == null)
return string.Empty;
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return string.Empty;
}
else
{
DescriptionAttribute attr = objs[0] as DescriptionAttribute;
return attr.Description;
}
}
#endregion
#region 枚举值输出描述
///
/// 枚举值输出描述
///
///
///
///
public static string EMToEnumDescriptionString(this int i)
{
//if (!(i > 0))
//{
// return string.Empty;
//}
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
throw new ArgumentException("enumItem requires a Enum ");
}
string name = System.Enum.GetName(enumType, i);
if (name == null)
return string.Empty;
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return string.Empty;
}
else
{
DescriptionAttribute attr = objs[0] as DescriptionAttribute;
return attr.Description;
}
}
#endregion
#region 枚举值输出描述
///
/// 枚举值输出描述
///
///
///
///
public static string EMToEnumDescriptionString(this string i)
{
//if (!(i > 0))
//{
// return string.Empty;
//}
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
throw new ArgumentException("enumItem requires a Enum ");
}
try
{
string name = i;
if (name == null)
return string.Empty;
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return string.Empty;
}
else
{
DescriptionAttribute attr = objs[0] as DescriptionAttribute;
return attr.Description;
}
}
catch
{
return string.Empty;
}
}
#endregion
#region 枚举值输出带标签描述
///
/// 枚举值输出带标签描述
///
///
///
///
public static string EMToEnumDescriptionWithLabelString(this Nullable i)
{
if (!(i > 0))
{
return string.Empty;
}
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
throw new ArgumentException("enumItem requires a Enum ");
}
string name = System.Enum.GetName(enumType, i);
return string.Format("{1} ", name.ToLower(), i.EMToEnumDescriptionString());
}
#endregion
#region 枚举值输出带标签描述
///
/// 枚举值输出带标签描述
///
///
///
///
public static string EMToEnumDescriptionWithLabelString(this int i)
{
return ((int?)i).EMToEnumDescriptionWithLabelString();
}
#endregion
#region 字符串转枚举
///
/// 字符串转枚举
///
///
///
///
public static T EMToEnum(this string i)
{
if (string.IsNullOrWhiteSpace(i))
{
return default(T);
}
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
throw new ArgumentException("enumItem requires a Enum ");
}
try
{
return (T)Enum.Parse(enumType, i);
}
catch
{
return default(T);
}
}
#endregion
#region 枚举值输出
///
/// 枚举值输出
///
///
///
public static string EMToEnumValueString(this System.Enum e)
{
Type enumType = e.GetType();
if (!enumType.IsEnum)
{
throw new ArgumentException("enumItem requires a Enum ");
}
string name = System.Enum.GetName(enumType, e);
if (name == null)
return string.Empty;
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return string.Empty;
}
else
{
return ((int)Enum.Parse(enumType, e.ToString())).ToString();
}
}
#endregion
}
}