using System; using System.Collections.Generic; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Threading.Tasks; namespace CMS.Plugin.HIAWms.Domain.Shared.Util { /// /// 导出帮助类. /// public static class ExportHelper { // 通用转换方法(兼容 Func>) public static Dictionary ConvertToExportData(T entity) { var dict = new Dictionary(); if (entity == null) return dict; foreach (var prop in typeof(T).GetProperties()) { var value = prop.GetValue(entity); dict[prop.Name] = FormatValue(value); } return dict; } // 批量转换方法 public static List> ConvertListToExportData(IEnumerable list) { return list.Select(ConvertToExportData).ToList(); } private static object FormatValue(object value) { if (value == null) return string.Empty; // 处理枚举 if (value is Enum enumValue) return GetEnumDescriptionUtil.GetEnumDescription(enumValue); // 处理布尔 if (value is bool boolValue) return boolValue ? "是" : "否"; return value; } private static string GetEnumDescription(Enum value) { var fieldInfo = value.GetType().GetField(value.ToString()); var description = fieldInfo? .GetCustomAttributes(typeof(DescriptionAttribute), false) .FirstOrDefault() as DescriptionAttribute; return description?.Description ?? value.ToString(); } } }