131 lines
4.2 KiB
C#
131 lines
4.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Concurrent;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace Face.Services.Extensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 枚举扩展
|
|||
|
|
/// </summary>
|
|||
|
|
public static class EnumerableExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 对枚举器的每个元素执行指定的操作
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">枚举器类型参数</typeparam>
|
|||
|
|
/// <param name="source">枚举器</param>
|
|||
|
|
/// <param name="action">要对枚举器的每个元素执行的委托</param>
|
|||
|
|
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
|||
|
|
{
|
|||
|
|
if (source.IsNullOrEmpty<T>() || action == null)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
foreach (T obj in source)
|
|||
|
|
{
|
|||
|
|
action(obj);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 指示指定的枚举器是null还是没有任何元素
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">枚举器类型参数</typeparam>
|
|||
|
|
/// <param name="source">要测试的枚举器</param>
|
|||
|
|
/// <returns>true:枚举器是null或者没有任何元素 false:枚举器不为null并且包含至少一个元素</returns>
|
|||
|
|
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
|
|||
|
|
{
|
|||
|
|
return source == null || !source.Any<T>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 得到枚举中文备注
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="enumValue"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GetEnumDesc(this System.Enum enumValue)
|
|||
|
|
{
|
|||
|
|
string value = enumValue.ToString();
|
|||
|
|
System.Reflection.FieldInfo field = enumValue.GetType().GetField(value);
|
|||
|
|
object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性
|
|||
|
|
if (objs.Length == 0) //当描述属性没有时,直接返回名称
|
|||
|
|
return value;
|
|||
|
|
DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
|
|||
|
|
return descriptionAttribute.Description;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string GetEnumDesc(this Type enumType, object val)
|
|||
|
|
{
|
|||
|
|
string enumvalue = Enum.GetName(enumType, val);
|
|||
|
|
if (string.IsNullOrEmpty(enumvalue))
|
|||
|
|
{
|
|||
|
|
return "";
|
|||
|
|
}
|
|||
|
|
FieldInfo finfo = enumType.GetField(enumvalue);
|
|||
|
|
object[] enumAttr = finfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
|
|||
|
|
if (enumAttr.Length > 0)
|
|||
|
|
{
|
|||
|
|
DescriptionAttribute desc = (DescriptionAttribute)enumAttr[0];
|
|||
|
|
if (desc != null)
|
|||
|
|
{
|
|||
|
|
return desc.Description;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return enumvalue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取所有的枚举描述和值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="type"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<EnumDto> GetEnumListItem(this Type type)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
List<EnumDto> list = new List<EnumDto>();
|
|||
|
|
|
|||
|
|
// 循环枚举获取所有的Fields
|
|||
|
|
foreach (var field in type.GetFields())
|
|||
|
|
{
|
|||
|
|
// 如果是枚举类型
|
|||
|
|
if (field.FieldType.IsEnum)
|
|||
|
|
{
|
|||
|
|
object tmp = field.GetValue(null);
|
|||
|
|
Enum enumValue = (Enum)tmp;
|
|||
|
|
int intValue = Convert.ToInt32(enumValue);
|
|||
|
|
string showName = enumValue.GetEnumDesc(); // 获取描述和排序
|
|||
|
|
list.Add(new EnumDto { Value = intValue, Description= showName });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//返回
|
|||
|
|
return list;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class EnumDto
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 枚举code
|
|||
|
|
/// </summary>
|
|||
|
|
public string Code { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 值
|
|||
|
|
/// </summary>
|
|||
|
|
public int Value { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 描述
|
|||
|
|
/// </summary>
|
|||
|
|
public string Description { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|