.Net反射-两种方式获取Enum中的值

public enum EJobType
{
客服 = , 业务员 = , 财务 = , 经理 =
}
Type jobType = typeof(EJobType);

方式1:

Array enumItems = Enum.GetValues(jobType);
foreach (var enumItem in enumItems)
{
int value = (int)enumItem;
string text = enumItem.ToString();
}

方式2:

FieldInfo[] fields = jobType.GetFields(BindingFlags.Static | BindingFlags.Public);//
foreach (var field in fields)
{
string text = field.Name;
object value = field.GetRawConstantValue();
}

再分享一个Enum的扩展:

 public static class EnumExtensions
{
/// <summary>
/// 枚举信息
/// </summary>
public class EnumInfo<T>
{
/// <summary>
/// 枚举名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 显示的名称
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// 值
/// </summary>
public T Value { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }
} /// <summary>
/// 获取枚举值的名称
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetName(this Enum value)
{
return Enum.GetName(value.GetType(), value);
}
/// <summary>
/// 获取该枚举的显示值(如果使用了DisplayAttribute 标签则显示描述中的别名,否则使用 Enum 的名称。)
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetText(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi == null)
{
return string.Empty;
} DisplayAttribute[] attributes = (DisplayAttribute[])fi.GetCustomAttributes(typeof(DisplayAttribute), false);
if ((attributes != null) && (attributes.Length > ))
return attributes[].Name;
else
return value.ToString();
} /// <summary>
/// 获取枚举代表的值
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int GetValue(this Enum value)
{
return Convert.ToInt32(value);
}
public static T Parse<T>(this Enum enumThis, int value)
{
return (T)Enum.Parse(enumThis.GetType(), value.ToString());
}
public static T Parse<T>(this Enum enumThis, string value)
{
return (T)Enum.Parse(enumThis.GetType(), value);
}
/// <summary>
/// 根据Name获取Value
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int GetEnumValue(Type enumType, string enumName)
{
return (int)Enum.Parse(enumType, enumName);
}
/// <summary>
/// 获取枚举信息(名称,值,描述)
/// </summary>
/// <returns></returns>
public static List<EnumInfo<T>> GetEnumInfo<T>(this Type enumType)
{
if (!enumType.IsEnum) throw new InvalidOperationException("type not a enum!");
List<EnumInfo<T>> emInfos = new List<EnumInfo<T>>();
var fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
foreach (var fi in fields)
{
EnumInfo<T> emInfo = new EnumInfo<T>();
emInfo.Name = fi.Name;
emInfo.Value = (T)fi.GetRawConstantValue();
var displayAttr = fi.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
if (displayAttr != null)
{
emInfo.DisplayName = ((DisplayAttribute)displayAttr).Name;
emInfo.Description = ((DisplayAttribute)displayAttr).Description;
}
var desAttr = fi.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();
if (desAttr != null)
{
emInfo.Description = ((DescriptionAttribute)desAttr).Description;
}
if (string.IsNullOrWhiteSpace(emInfo.Description))
{
emInfo.Description = fi.Name;
}
if (string.IsNullOrWhiteSpace(emInfo.DisplayName))
{
emInfo.DisplayName = fi.Name;
}
emInfos.Add(emInfo);
}
return emInfos;
}
public static List<EnumInfo<object>> GetEnumInfo(this Type enumType)
{
return GetEnumInfo<object>(enumType);
}
}
上一篇:HDU1003前导和


下一篇:**CI两种方式查询所返回的结果数量