using Org.BouncyCastle.Asn1.X509.Qualified;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
namespace ZJ_BYD.Untils
{
public class EnumHelper
{
///
/// 根据值获取枚举方法
///
///
///
///
public static Enum GetEnumByValue(Type enumType, int value)
{
try
{
var flag = Enum.IsDefined(enumType, value);
if (flag)
{
return Enum.Parse(enumType, Enum.GetName(enumType, value)) as Enum;
}
else
{
return null;
}
}
catch (Exception ex)
{
var errMsg = ex == null ? "未知异常" : ex.Message;
MessageBox.Show($"GetEnumDescription方法出发异常:{errMsg},枚举值={value}请联系管理员!");
return null;
}
}
///
/// 获取枚举的Description
///
///
///
public static string GetEnumDescription(Enum enumValue)
{
try
{
string value = enumValue.ToString();
FieldInfo field = enumValue.GetType().GetField(value);
object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性
if (objs == null || objs.Length == 0) //当描述属性没有时,直接返回名称
return value;
DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
return descriptionAttribute.Description;
}
catch (Exception ex)
{
var errMsg = ex == null ? "未知异常" : ex.Message;
MessageBox.Show($"GetEnumDescription方法出发异常:{errMsg},枚举值={enumValue},请联系管理员!");
return "";
}
}
}
}