33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace TempTask.WebEntry.Tools
|
|
{
|
|
public class EnumHelper
|
|
{
|
|
/// <summary>
|
|
/// 得到枚举的DescriptionAttribute值。
|
|
/// </summary>
|
|
/// <typeparam name="TEnum"></typeparam>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
static public string GetEnumDescription<TEnum>(object value)
|
|
{
|
|
Type enumType = typeof(TEnum);
|
|
if (!enumType.IsEnum)
|
|
throw new ArgumentException("不是枚举类型");
|
|
var name = Enum.GetName(enumType, value);
|
|
if (name == null)
|
|
return string.Empty;
|
|
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (objs == null || objs.Length == 0)
|
|
return string.Empty;
|
|
DescriptionAttribute attr = objs[0] as DescriptionAttribute;
|
|
return attr.Description;
|
|
}
|
|
}
|
|
}
|