first commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Infrastructure
|
||||
{
|
||||
public static class EnumExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取枚举描述属性值
|
||||
/// </summary>
|
||||
/// <param name="enum"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDescription(this Enum @enum)
|
||||
{
|
||||
Type type = @enum.GetType();
|
||||
string name = Enum.GetName(type, @enum);
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return @enum.ToString();
|
||||
|
||||
FieldInfo field = type.GetField(name);
|
||||
DescriptionAttribute des = field?.GetCustomAttribute<DescriptionAttribute>();
|
||||
if (des == null)
|
||||
return @enum.ToString();
|
||||
|
||||
return des.Description;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user