using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; namespace MAX.Core.Helpers; /// /// 反射操作 /// public static class ReflectionHelper { /// /// 获取类型描述,使用DescriptionAttribute设置描述 /// /// 类型 public static string GetDescription() { return GetDescription(Common.GetType()); } /// /// 获取类型成员描述,使用DescriptionAttribute设置描述 /// /// 类型 /// 成员名称 public static string GetDescription(string memberName) { return GetDescription(Common.GetType(), memberName); } /// /// 获取类型成员描述,使用DescriptionAttribute设置描述 /// /// 类型 /// 成员名称 public static string GetDescription(Type type, string memberName) { if (type == null) { return string.Empty; } if (string.IsNullOrWhiteSpace(memberName)) { return string.Empty; } return GetDescription(type.GetTypeInfo().GetMember(memberName).FirstOrDefault()); } /// /// 获取类型成员描述,使用DescriptionAttribute设置描述 /// /// 成员 public static string GetDescription(MemberInfo member) { if (member == null) { return string.Empty; } return member.GetCustomAttribute() is DescriptionAttribute attribute ? attribute.Description : member.Name; } /// /// 获取显示名称,使用DisplayNameAttribute设置显示名称 /// /// 类型 public static string GetDisplayName() { return GetDisplayName(Common.GetType()); } /// /// 获取显示名称,使用DisplayAttribute或DisplayNameAttribute设置显示名称 /// public static string GetDisplayName(MemberInfo member) { if (member == null) { return string.Empty; } if (member.GetCustomAttribute() is DisplayAttribute displayAttribute) { return displayAttribute.Name; } if (member.GetCustomAttribute() is DisplayNameAttribute displayNameAttribute) { return displayNameAttribute.DisplayName; } return string.Empty; } /// /// 获取显示名称或描述,使用DisplayNameAttribute设置显示名称,使用DescriptionAttribute设置描述 /// /// 类型 public static string GetDisplayNameOrDescription() { return GetDisplayNameOrDescription(Common.GetType()); } /// /// 获取属性显示名称或描述,使用DisplayAttribute或DisplayNameAttribute设置显示名称,使用DescriptionAttribute设置描述 /// public static string GetDisplayNameOrDescription(MemberInfo member) { string result = GetDisplayName(member); return string.IsNullOrWhiteSpace(result) ? GetDescription(member) : result; } /// /// 查找类型列表 /// /// 查找类型 /// 待查找的程序集列表 public static List FindTypes(params Assembly[] assemblies) { Type findType = typeof(TFind); return FindTypes(findType, assemblies); } /// /// 查找类型列表 /// /// 查找类型 /// 待查找的程序集列表 public static List FindTypes(Type findType, params Assembly[] assemblies) { List result = []; foreach (Assembly assembly in assemblies) { result.AddRange(GetTypes(findType, assembly)); } return result.Distinct().ToList(); } /// /// 获取类型列表 /// private static List GetTypes(Type findType, Assembly assembly) { List result = []; if (assembly == null) { return result; } Type[] types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { Debug.WriteLine(ex); return result; } foreach (Type type in types) { AddType(result, findType, type); } return result; } /// /// 添加类型 /// private static void AddType(List result, Type findType, Type type) { if (type.IsInterface || type.IsAbstract) { return; } if (findType.IsAssignableFrom(type) == false && MatchGeneric(findType, type) == false) { return; } result.Add(type); } /// /// 泛型匹配 /// private static bool MatchGeneric(Type findType, Type type) { if (findType.IsGenericTypeDefinition == false) { return false; } Type definition = findType.GetGenericTypeDefinition(); foreach (Type implementedInterface in type.FindInterfaces((filter, criteria) => true, null)) { if (implementedInterface.IsGenericType == false) { continue; } return definition.IsAssignableFrom(implementedInterface.GetGenericTypeDefinition()); } return false; } /// /// 获取实现了接口的所有实例 /// /// 接口类型 /// 待查找的程序集列表 public static List GetInstancesByInterface(params Assembly[] assemblies) { return FindTypes(assemblies) .Select(t => CreateInstance(t)).ToList(); } /// /// 动态创建实例 /// /// 目标类型 /// 类型 /// 传递给构造函数的参数 public static T CreateInstance(Type type, params object[] parameters) { return Convert.To(Activator.CreateInstance(type, parameters)); } public static Type GetType(string typeName) { Type type = null; Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies(); int assemblyArrayLength = assemblyArray.Length; for (int i = 0; i < assemblyArrayLength; ++i) { type = assemblyArray[i].GetType(typeName); if (type != null) { return type; } } for (int i = 0; i < assemblyArrayLength; ++i) { Type[] typeArray = assemblyArray[i].GetTypes(); int typeArrayLength = typeArray.Length; for (int j = 0; j < typeArrayLength; ++j) { if (typeArray[j].Name.Equals(typeName)) { return typeArray[j]; } } } return type; } /// /// 获取程序集 /// /// 程序集名称 public static Assembly GetAssembly(string assemblyName) { return Assembly.Load(new AssemblyName(assemblyName)); } /// /// 是否布尔类型 /// /// 成员 public static bool IsBool(MemberInfo member) { if (member == null) { return false; } return member.MemberType switch { MemberTypes.TypeInfo => member.ToString() == "System.Boolean", MemberTypes.Property => IsBool((PropertyInfo)member), _ => false, }; } /// /// 是否布尔类型 /// private static bool IsBool(PropertyInfo property) { return property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?); } /// /// 是否枚举类型 /// /// 成员 public static bool IsEnum(MemberInfo member) { if (member == null) { return false; } return member.MemberType switch { MemberTypes.TypeInfo => ((TypeInfo)member).IsEnum, MemberTypes.Property => IsEnum((PropertyInfo)member), _ => false, }; } /// /// 是否枚举类型 /// private static bool IsEnum(PropertyInfo property) { if (property.PropertyType.GetTypeInfo().IsEnum) { return true; } Type value = Nullable.GetUnderlyingType(property.PropertyType); if (value == null) { return false; } return value.GetTypeInfo().IsEnum; } /// /// 是否日期类型 /// /// 成员 public static bool IsDate(MemberInfo member) { if (member == null) { return false; } return member.MemberType switch { MemberTypes.TypeInfo => member.ToString() == "System.DateTime", MemberTypes.Property => IsDate((PropertyInfo)member), _ => false, }; } /// /// 是否日期类型 /// private static bool IsDate(PropertyInfo property) { if (property.PropertyType == typeof(DateTime)) { return true; } if (property.PropertyType == typeof(DateTime?)) { return true; } return false; } /// /// 是否整型 /// /// 成员 public static bool IsInt(MemberInfo member) { if (member == null) { return false; } return member.MemberType switch { MemberTypes.TypeInfo => member.ToString() is "System.Int32" or "System.Int16" or "System.Int64", MemberTypes.Property => IsInt((PropertyInfo)member), _ => false, }; } /// /// 是否整型 /// private static bool IsInt(PropertyInfo property) { if (property.PropertyType == typeof(int)) { return true; } if (property.PropertyType == typeof(int?)) { return true; } if (property.PropertyType == typeof(short)) { return true; } if (property.PropertyType == typeof(short?)) { return true; } if (property.PropertyType == typeof(long)) { return true; } if (property.PropertyType == typeof(long?)) { return true; } return false; } /// /// 是否数值类型 /// /// 成员 public static bool IsNumber(MemberInfo member) { if (member == null) { return false; } if (IsInt(member)) { return true; } return member.MemberType switch { MemberTypes.TypeInfo => member.ToString() is "System.Double" or "System.Decimal" or "System.Single", MemberTypes.Property => IsNumber((PropertyInfo)member), _ => false, }; } /// /// 是否数值类型 /// private static bool IsNumber(PropertyInfo property) { if (property.PropertyType == typeof(double)) { return true; } if (property.PropertyType == typeof(double?)) { return true; } if (property.PropertyType == typeof(decimal)) { return true; } if (property.PropertyType == typeof(decimal?)) { return true; } if (property.PropertyType == typeof(float)) { return true; } if (property.PropertyType == typeof(float?)) { return true; } return false; } /// /// 是否集合 /// /// 类型 public static bool IsCollection(Type type) { if (type.IsArray) { return true; } return IsGenericCollection(type); } /// /// 是否泛型集合 /// /// 类型 public static bool IsGenericCollection(Type type) { if (!type.IsGenericType) { return false; } Type typeDefinition = type.GetGenericTypeDefinition(); return typeDefinition == typeof(IEnumerable<>) || typeDefinition == typeof(IReadOnlyCollection<>) || typeDefinition == typeof(IReadOnlyList<>) || typeDefinition == typeof(ICollection<>) || typeDefinition == typeof(IList<>) || typeDefinition == typeof(List<>); } /// /// 从目录中获取所有程序集 /// /// 目录绝对路径 public static List GetAssemblies(string directoryPath) { return Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories).ToList() .Where(t => t.EndsWith(".exe") || t.EndsWith(".dll")) .Select(path => Assembly.Load(new AssemblyName(path))).ToList(); } /// /// 获取公共属性列表 /// /// 实例 public static List GetPublicProperties(object instance) { PropertyInfo[] properties = instance.GetType().GetProperties(); return properties.ToList().Select(t => new CommonItem(t.Name, t.GetValue(instance))).ToList(); } /// /// 获取顶级基类 /// /// 类型 public static Type GetTopBaseType() { return GetTopBaseType(typeof(T)); } /// /// 获取顶级基类 /// /// 类型 public static Type GetTopBaseType(Type type) { if (type == null) { return null; } if (type.IsInterface) { return type; } if (type.BaseType == typeof(object)) { return type; } return GetTopBaseType(type.BaseType); } /// /// 获取元素类型,如果是集合,返回集合的元素类型 /// /// 类型 public static Type GetElementType(Type type) { if (IsCollection(type) == false) { return type; } if (type.IsArray) { return type.GetElementType(); } Type[] genericArgumentsTypes = type.GetTypeInfo().GetGenericArguments(); if (genericArgumentsTypes == null || genericArgumentsTypes.Length == 0) { throw new ArgumentException("泛型类型参数不能为空"); } return genericArgumentsTypes[0]; } }