添加项目文件。
This commit is contained in:
248
Common/EnumHelper.cs
Normal file
248
Common/EnumHelper.cs
Normal file
@ -0,0 +1,248 @@
|
||||
|
||||
using System;
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
|
||||
namespace Epost.Common
|
||||
{
|
||||
public static class EnumHelper
|
||||
{
|
||||
#region 枚举描述输出
|
||||
/// <summary>
|
||||
/// 枚举描述输出
|
||||
/// </summary>
|
||||
/// <param name="e"></param>
|
||||
/// <returns></returns>
|
||||
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 枚举值输出描述
|
||||
/// <summary>
|
||||
/// 枚举值输出描述
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="i"></param>
|
||||
/// <returns></returns>
|
||||
public static string EMToEnumDescriptionString<T>(this Nullable<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 枚举值输出描述
|
||||
/// <summary>
|
||||
/// 枚举值输出描述
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="i"></param>
|
||||
/// <returns></returns>
|
||||
public static string EMToEnumDescriptionString<T>(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 枚举值输出描述
|
||||
/// <summary>
|
||||
/// 枚举值输出描述
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="i"></param>
|
||||
/// <returns></returns>
|
||||
public static string EMToEnumDescriptionString<T>(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 枚举值输出带标签描述
|
||||
/// <summary>
|
||||
/// 枚举值输出带标签描述
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="i"></param>
|
||||
/// <returns></returns>
|
||||
public static string EMToEnumDescriptionWithLabelString<T>(this Nullable<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);
|
||||
return string.Format("<span class=\"label label-{0}\">{1}</span> ", name.ToLower(), i.EMToEnumDescriptionString<T>());
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 枚举值输出带标签描述
|
||||
/// <summary>
|
||||
/// 枚举值输出带标签描述
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="i"></param>
|
||||
/// <returns></returns>
|
||||
public static string EMToEnumDescriptionWithLabelString<T>(this int i)
|
||||
{
|
||||
return ((int?)i).EMToEnumDescriptionWithLabelString<T>();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 字符串转枚举
|
||||
/// <summary>
|
||||
/// 字符串转枚举
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="i"></param>
|
||||
/// <returns></returns>
|
||||
public static T EMToEnum<T>(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 枚举值输出
|
||||
/// <summary>
|
||||
/// 枚举值输出
|
||||
/// </summary>
|
||||
/// <param name="e"></param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user