Criar uma classe Enum como a de baixo
using
System.ComponentModel;
using
System;
using
System.Reflection;
namespace
XXXXXXX.yyy
{
public enum NomeDaClasseEnum
{
[Description("String bla bla bla.")]
blablabla = 1,
}
}
public
static class EnumHelper
{
/// <summary>
/// Retrieve the description on the enum, e.g.
/// [Description("String
bla bla bla ")]
/// blablabla = 1,
/// Then when you pass in the enum, it will retrieve the
description
/// </summary>
/// <param
name="en">The Enumeration</param>
/// <returns>A string representing the friendly name</returns>
public static string
GetDescription(Enum en)
{
Type
type = en.GetType();
MemberInfo[]
memInfo = type.GetMember(en.ToString());
if
(memInfo != null && memInfo.Length >
0)
{
object[] attrs =
memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if
(attrs != null && attrs.Length > 0)
{
return
((DescriptionAttribute)attrs[0]).Description;
}
}
return
en.ToString();
}
}
A descrição será através da linha
String exemplo = EnumHelper.GetDescription(NomeDaClasseEnum.blablabla)
Nenhum comentário:
Postar um comentário