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.
120 lines
3.8 KiB
120 lines
3.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace Znyc.Cloudcar.Admin.Commons.Extensions
|
|
{
|
|
/// <summary>
|
|
/// 枚举类型的扩展
|
|
/// </summary>
|
|
public static class EnumExtensions
|
|
{
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public static string ToDescription(this Enum item)
|
|
{
|
|
string name = item.ToString();
|
|
DescriptionAttribute desc = item.GetType().GetField(name)?.GetCustomAttribute<DescriptionAttribute>();
|
|
return desc?.Description ?? name;
|
|
}
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 根据 value 值获取Description
|
|
/// </summary>
|
|
/// <param name="enumType"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static string GetDescription(this Type enumType, int value)
|
|
{
|
|
object Key = GetNameAndValue(enumType).FirstOrDefault(p => p.Value.Equals(value)).Key;
|
|
if (Key==null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return Key.ToString();
|
|
}
|
|
/// <summary>
|
|
/// 获取枚举名以及对应的Value
|
|
/// </summary>
|
|
/// <param name="type">枚举类型typeof(T)</param>
|
|
/// <returns>返回Dictionary ,Key为描述名, Value为枚举对应的值</returns>
|
|
public static Dictionary<object, object> GetNameAndValue(this Type type)
|
|
{
|
|
if (type.IsEnum)
|
|
{
|
|
Dictionary<object, object> dic = new Dictionary<object, object>();
|
|
Array enumValues = Enum.GetValues(type);
|
|
foreach (Enum value in enumValues)
|
|
{
|
|
dic.Add(GetDescription(value), value.GetHashCode());
|
|
}
|
|
|
|
return dic;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从枚举中获取Description
|
|
/// </summary>
|
|
/// <param name="enumName">需要获取枚举描述的枚举</param>
|
|
/// <returns>描述内容</returns>
|
|
public static string GetDescription(this Enum enumName)
|
|
{
|
|
string description = string.Empty;
|
|
FieldInfo fieldInfo = enumName.GetType().GetField(enumName.ToString());
|
|
DescriptionAttribute[] attributes = GetDescriptAttr(fieldInfo);
|
|
if (attributes != null && attributes.Length > 0)
|
|
{
|
|
description = attributes[0].Description;
|
|
}
|
|
else
|
|
{
|
|
description = enumName.ToString();
|
|
}
|
|
|
|
return description;
|
|
}
|
|
/// <summary>
|
|
/// 获取字段Description
|
|
/// </summary>
|
|
/// <param name="fieldInfo">FieldInfo</param>
|
|
/// <returns>DescriptionAttribute[] </returns>
|
|
private static DescriptionAttribute[] GetDescriptAttr(FieldInfo fieldInfo)
|
|
{
|
|
if (fieldInfo != null)
|
|
{
|
|
return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|