1.枚举类引入命名空间 System.ComponentModel.DataAnnotations
using System.ComponentModel.DataAnnotations
public enum Sex
{
[Display(Name="男")]
Male=1,
[Display(Name="女")]
Female=2,
[Display(Name="未知")]
None=-1
}
2.自定义枚举扩展方法
using System
public static class EnumExtensions
{
pubic static T GetEnumByDisplayName<T>(string displayName) where T:struct
{
Type type=typeof(T);
string[] names=Enum.GetNames(type);
string[] array=names;
foreach(string text in array)
{
DisplayAttribute customAttribute = type.GetField(text).GetCustomAttribute<DisplayAttribute>();
T result;
if(customAttribute == null)
{
if(text==displayName && Enum.TryParse(text,out result)
{