You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.2 KiB
C#

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
{
/// <summary>
/// 根据值获取枚举方法
/// </summary>
/// <param name="enumType"></param>
/// <param name="value"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 获取枚举的Description
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
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 "";
}
}
}
}