获取枚举值上的Description特性说明


/// <summary>
/// 获取枚举值上的Description特性说明
/// </summary>
/// <typeparam name="T">枚举类型</typeparam>
/// <param name="obj">枚举值</param>
/// <returns></returns>
public static string GetDescription<T>(T obj)
{
    Type type = obj.GetType();
    FieldInfo field = type.GetField(Enum.GetName(type, obj));
    DescriptionAttribute desc = Attribute.GetCustomAttribute
    (field, typeof(DescriptionAttribute)) as DescriptionAttribute;
    if(desc == null)
    {
        return string.Empty;
    }
 
    return desc.Description;
}

 

 
posted @ 2015-09-06 11:17  落一叶而知秋  阅读(493)  评论(0编辑  收藏  举报