公司项目用到枚举封装的帮助类,加上枚举确实很方便直观,管理一些特定值,但有时候就是想知道具体枚举值,如果用传统if else或是swtich就太low,这里采用更优雅,逼格更高的一种方式,直接获取枚举,或是枚举值等等,甚至可以获取枚举上面特性描述字符串等等
关键点:
第一:可以给枚举扩展方法,获取特性,静态类,静态方法,通过枚举type获取fieldinfo,来获取上面的特性实例
第二: 通过特定枚举获取对应枚举值,或是通过对应枚举值获取枚举Enum.GetValues()还有方法Enum.Format(enumtype,enum,format)
第三:通过描述获取枚举值,调用以上方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace TestAttributeEnum
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine((StatusEnum.Delete.GetDescription()));
Console.WriteLine(EnumExtension.GetEnumByDescription(typeof(StatusEnum),"删除"));
Console.WriteLine(EnumExtension.GetEnumSum(typeof(StatusEnum)));
Console.WriteLine(EnumExtension.GetEnumName<StatusEnum>(1));
Console.WriteLine(EnumExtension.GetEnumValue<StatusEnum>("Delete"));
}
}
public enum StatusEnum
{
[Description("修改")]
Update = 1,
[Description("新增")]
Insert = 2,
[Description("删除")]
Delete = 3
}
public class RemarkAttribute : Attribute
{
public string _remark { get; set; }
public RemarkAttribute(string remark)
{
_remark = remark;
}
}
//给枚举增加一个扩展方法,
public static class EnumExtension
{
public static string GetDescription(this StatusEnum statusEnum)
{
FieldInfo fieldInfo = typeof(StatusEnum).GetField(statusEnum.ToString());
if (fieldInfo==null)
{
return string.Empty;
}
DescriptionAttribute descriptionAttribute=(DescriptionAttribute)fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
return descriptionAttribute?.Description ?? string.Empty;
}
public static string GetRemark(this StatusEnum statusEnum)
{
FieldInfo fieldInfo = typeof(StatusEnum).GetField(statusEnum.ToString());
if (fieldInfo == null)
{
return string.Empty;
}
RemarkAttribute remarkAttribute = (RemarkAttribute)fieldInfo.GetCustomAttributes(typeof(RemarkAttribute), false)[0];
return remarkAttribute?._remark ?? string.Empty;
}
//从给定的枚举类型Type中,通过枚举描述找到对应的枚举值,
public static Enum GetEnumByDescription(Type enumType,string description)
{
if (string.IsNullOrEmpty(description))
{
return null;
}
Array values = Enum.GetValues(enumType);
foreach (StatusEnum item in values)
{
if (description==GetDescription(item))
{
return item;
}
}
return null;
}
//获取给定枚举类型所有的int值的和
public static int GetEnumSum(Type enumType)
{
Array values = Enum.GetValues(enumType);
int defaultValue = 0;
foreach (StatusEnum item in values)
{
defaultValue +=Convert.ToInt32(Enum.Format(enumType,item,"d"));
}
return defaultValue;
}
/// <summary>
/// 根据传入的int返回对应枚举属性名称
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="num">进制</param>
/// <returns></returns>
public static string GetEnumName<T>(int value)
{
string name = "";
name = Enum.Parse(typeof(T), Enum.GetName(typeof(T), value)).ToString();
return name;
}
/// <summary>
/// 根据传入的枚举属性获得对应值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static int GetEnumValue<T>(string value)
{
Type type = typeof(T);
var schoolId = Enum.Format(type, Enum.Parse(type, value), "d");
return Convert.ToInt32(schoolId);
}
}
}
参考https://www.cnblogs.com/zhangxiaoyong/p/7452408.html,