Enumeration class attribute name Package

internal class NameAttribute : Attribute
{
private string _name;
public NameAttribute(string _name)
{
this._name = _name;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}

 

 

public static class EnumsHelper
{

enum ProductStatus public
{
/// <Summary>
/// Returns the
/// </ Summary>
[the Name ( "audit is not")]
NotAuditPass = -1,
/// <Summary>
/// pending
@ / </ Summary>
[the Name ( "awaiting approval")]
WaitProcess = 0,
/// <Summary>
/// approval
/// </ Summary>
[the Name ( "approval")]
AuditPass =. 1,

}

 

public static string GetEnumName(Enum _enum)
{

Type type = _enum.GetType();
FieldInfo fd = type.GetField(_enum.ToString());
if (fd == null) return string.Empty;
object[] attrs = fd.GetCustomAttributes(typeof(NameAttribute), false);
string name = string.Empty;
foreach (NameAttribute attr in attrs)
{
name = attr.Name;
}
return name;
}
/// <summary>
/// 获得枚举类型数据项(不包括空项)
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <returns></returns>
public static IList<object> GetItems(this Type enumType)
{
if (!enumType.IsEnum)
throw new InvalidOperationException();

IList<object> list = new List<object>();

// Get an enumeration field
var = enumType.GetEnumValues values ();
the foreach (values in Item var)
{
// add to the list
list.Add item item (new {Value = (int), Text = GetEnumName ((Enum)) });

}
return list;
}

}

 

Guess you like

Origin www.cnblogs.com/xiaoxiangpaou/p/10930404.html