38 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.ComponentModel;
 | |
| using System.Linq;
 | |
| using System.Reflection;
 | |
| 
 | |
| namespace IRaCIS.Core.Infrastructure.Extention
 | |
| {
 | |
|     public static class EnumToSelectExtension
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// 将枚举转换成字典(枚举,自定义描述)
 | |
|         /// </summary>
 | |
|         /// <typeparam name="TEnum"></typeparam>
 | |
|         /// <param name="exceptList">需要排除的枚举</param>
 | |
|         /// <returns></returns>
 | |
|         public static Dictionary<object, string> ToSelect<TEnum>(  params TEnum[] exceptList) where TEnum : struct, Enum
 | |
|         {
 | |
|             var type = typeof(TEnum);
 | |
|             var dict = new Dictionary<object, string>();
 | |
|             foreach (var value in Enum.GetValues<TEnum>())
 | |
|             //foreach (var value in type.GetEnumValues())
 | |
|             {
 | |
|                 var attr = type.GetField(value.ToString())
 | |
|                     .GetCustomAttribute<DescriptionAttribute>();
 | |
|                 if (attr is null || exceptList.Contains(value)) continue;
 | |
| 
 | |
|                 var key = type.GetFields().FirstOrDefault(t=>t.Name== value.ToString()).GetRawConstantValue();
 | |
|                 dict[key] = attr.Description;
 | |
|             }
 | |
|             return dict;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|    
 | |
| 
 | |
| }
 |