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

32 lines
987 B

using System.ComponentModel;
using System.Reflection;
namespace Znyc.CloudCar.Utility.Extensions
{
public static class EnumExtensions
{
public static string ToDescription(this Enum item)
{
string name = item.ToString();
DescriptionAttribute desc = item.GetType().GetField(name)?.GetCustomAttribute<DescriptionAttribute>();
return desc?.Description ?? name;
}
public static long ToInt64(this Enum item)
{
return Convert.ToInt64(item);
}
/// <summary>
/// 根据枚举的value获取枚举
/// </summary>
/// <param name="enumType">枚举的类型,示例:typeof(enum1)</param>
/// <param name="value">可能的枚举值</param>
/// <returns>枚举,示例:enum1.en1</returns>
public static Enum ParseEnum(Type enumType, string value)
{
return Enum.Parse(enumType, value) as Enum;
}
}
}