using KissUtil.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
namespace CMS.Plugin.HIAWms.Domain.Shared.Util
{
///
/// 枚举帮助类.
///
public static class EnumHelper
{
///
/// 获取枚举.
///
///
///
///
public static List GetEnumItems(string enumName)
{
// 获取当前程序集中的所有类型
var enumType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.FirstOrDefault(t => t.Name == enumName && t.IsEnum);
if (enumType == null)
{
throw new UserFriendlyException($"枚举类型 {enumName} 不存在");
}
return GetEnumItems(enumType);
}
///
/// 获取枚举类型.
///
///
///
///
public static List GetEnumItems(Type enumType)
{
if (!enumType.IsEnum)
{
throw new UserFriendlyException("提供的类型不是枚举类型");
}
var result = new List();
var values = Enum.GetValues(enumType);
foreach (var value in values)
{
var fieldInfo = enumType.GetField(value.ToString());
var description = fieldInfo?
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() as DescriptionAttribute;
result.Add(new EnumItem
{
Value = (int)value,
Name = value.ToString(),
Description = description?.Description ?? value.ToString(),
});
}
return result;
}
}
///
/// 枚举描述.
///
public class EnumItem
{
public int Value { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}