返回一个枚举值,该值指示指定枚举的描述名称中存在给定的整数值或其字符串名称,如若不存在则返回指定默认值
/// <summary>
/// 返回一个枚举值,该值指示指定枚举的描述名称中存在给定的整数值或其字符串名称,如若不存在则返回指定默认值。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static T GetEnumByDescription<T>(string value,T defaultValue) where T : struct, Enum
{
var list = GetEnumItems2<T>();
var m = list.FirstOrDefault(i => i.Description == value);
if (m == null)
{
return defaultValue;
}
return m.Enum;
}
public static List<EnumItem2<T>> GetEnumItems2<T>() where T : struct, Enum
{
List<EnumItem2<T>> itemList = new List<EnumItem2<T>>();
foreach (var v in Enum.GetValues<T>())
{
EnumItem2<T> item = new EnumItem2<T>();
item.EnumKey = Convert.ToInt32(v);
item.EnumValue = v.ToString();
item.Description = GetDescription(v);
item.Enum = v;
itemList.Add(item);
}
return itemList;
}
public class EnumItem2<T>
{
public int EnumKey { get; set; }
public string EnumValue { get; set; }
public string Description { get; set; }
public T Enum { get; set; }
}
枚举转换下拉选项
SelectListItem
需要安装Microsoft.AspNetCore.Mvc.ViewFeatures
并引用Microsoft.AspNetCore.Mvc.Rendering
/// <summary>
/// 返回Razor可识别的下拉框集合
/// </summary>
/// <param name="enumType"></param>
/// <param name="selectValue"></param>
/// <param name="removes"></param>
/// <returns></returns>
public static IList<SelectListItem> ToSelectList(Type enumType, int selectValue, params int[] removes)
{
IList<SelectListItem> listItem = new List<SelectListItem>();
if (enumType.IsEnum)
{
Array values = Enum.GetValues(enumType);
if (values.Length > 0)
{
foreach (int item in values)
{
if (removes.Length > 0)
{
if (removes.Contains(item))
{
continue;
}
}
var test = System.Enum.GetName(enumType, item);
FieldInfo fi = enumType.GetField(test);
var dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if (dna != null && !string.IsNullOrEmpty(dna.Description))
{
test = dna.Description;
}
listItem.Add(new SelectListItem { Value = item.ToString(), Text = test, Selected = selectValue == item });
}
}
}
else
{
throw new ArgumentException("请传入正确的枚举!");
}
return listItem;
}
获取枚举描述信息
/// <summary>
/// 获取描述信息
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetDescription(this Enum obj)
{
if (obj == null)
{
return string.Empty;
}
try
{
Type enumType = obj.GetType();
FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj));
var dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
return dna.Description;
}
catch
{
return string.Empty;
}
return obj.ToString();
}
双属性描述设置
/// <summary>
/// 枚举双描述属性
/// </summary>
public class BaseDescriptionAttribute : DescriptionAttribute
{
public BaseDescriptionAttribute(string description1) : base(description1) { }
public BaseDescriptionAttribute(string description1, string description2) : base(description1)
{
this.Description2 = description2;
}
public string Description2 { get; set; }
}
双属性使用
public enum AccessStatus
{
[BaseDescription("正常", "正常")]
Normal,
}
获取双属性描述信息
/// <summary>
/// 双属性扩展方法,获得枚举的Description
/// </summary>
/// <param name="value">枚举值</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>枚举的Description</returns>
public static EnumItem GetBaseDescription(this Enum value, bool nameInstead = true)
{
EnumItem e = new EnumItem();
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
BaseDescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(BaseDescriptionAttribute)) as BaseDescriptionAttribute;
if (attribute == null && nameInstead == true)
{
e.Description1 = name;
e.Description2 = name;
return e;
}
else
{
e.Description1 = attribute.Description;
e.Description2 = attribute.Description2;
return e;
}
}
public class EnumItem
{
public int EnumKey { get; set; }
public string EnumValue { get; set; }
public string Description1 { get; set; }
public string Description2 { get; set; }
}
/// <summary>
/// 获取枚举的相关信息
/// </summary>
/// <param name="e">枚举的类型</param>
/// <returns></returns>
public static List<EnumItem> GetBaseEnumItems(Type e)
{
List<EnumItem> itemList = new List<EnumItem>();
foreach (Enum v in Enum.GetValues(e))
{
EnumItem item = new EnumItem();
item.EnumKey = Convert.ToInt32(v);
item.EnumValue = v.ToString();
item.Description1 = GetBaseDescription(v).Description1;
item.Description2 = GetBaseDescription(v).Description2;
itemList.Add(item);
}
return itemList;
}
网友评论