using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace MyCode.Project.Infrastructure.Common { public static class Reflection { #region GetDescription(获取类型描述) /// /// 获取类型描述,使用设置描述 /// /// 类型 /// public static string GetDescription() { return GetDescription(GetType()); } /// /// 获取类型成员描述,使用设置描述 /// /// 类型 /// 成员名称 /// public static string GetDescription(string memberName) { return GetDescription(GetType(), memberName); } /// /// 获取类型成员描述,使用设置描述 /// /// 类型 /// 成员名称 /// public static string GetDescription(Type type, string memberName) { if (type == null) { return string.Empty; } return string.IsNullOrEmpty(memberName) ? string.Empty : GetDescription(type.GetTypeInfo().GetMember(memberName).FirstOrDefault()); } /// /// 获取类型成员描述,使用设置描述 /// /// 成员 /// public static string GetDescription(MemberInfo member) { if (member == null) { return string.Empty; } var attribute = member.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute == null ? member.Name : attribute.Description; } #endregion #region GetDisplayName(获取类型显示名称) /// /// 获取类型显示名称,使用设置显示名称 /// /// 类型 /// public static string GetDisplayName() { return GetDisplayName(GetType()); } /// /// 获取类型显示名称,使用设置显示名称 /// /// 类型 /// private static string GetDisplayName(Type type) { if (type == null) { return string.Empty; } var attribute = type.GetCustomAttribute(typeof(DisplayNameAttribute)) as DisplayNameAttribute; return attribute != null ? attribute.DisplayName : string.Empty; } /// /// 获取类型成员显示名称,,使用设置显示名称 /// /// 成员 /// private static string GetDisplayName(MemberInfo member) { if (member == null) { return string.Empty; } var displayNameAttribute = member.GetCustomAttribute(typeof(DisplayNameAttribute)) as DisplayNameAttribute; if (displayNameAttribute != null) { return displayNameAttribute.DisplayName; } var displayAttribute = member.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute; if (displayAttribute == null) { return string.Empty; } return displayAttribute.Description; } #endregion #region GetDisplayNameOrDescription(获取显示名称或类型描述) /// /// 获取类型显示名称或描述,使用设置描述,使用设置显示名称 /// /// 类型 /// public static string GetDisplayNameOrDescription() { var type = GetType(); var result = GetDisplayName(type); if (string.IsNullOrEmpty(result)) { result = GetDescription(type); } return result; } /// /// 获取类型显示名称或成员描述,使用设置描述,使用设置显示名称 /// /// 成员 /// public static string GetDisplayNameOrDescription(MemberInfo member) { var result = GetDisplayName(member); if (!string.IsNullOrEmpty(result)) { return result; } return GetDescription(member); } #endregion #region GetTypesByInterface(获取实现了接口的所有具体类型) /// /// 获取实现了接口的所有具体类型 /// /// 接口类型 /// 在该程序集中查找 /// public static List GetTypesByInterface(Assembly assembly) { var typeInterface = typeof(TInterface); return assembly.GetTypes() .Where( t => typeInterface.GetTypeInfo().IsAssignableFrom(t) && t != typeInterface && t.GetTypeInfo().IsAbstract == false) .Select(t => CreateInstance(t)) .ToList(); } #endregion #region CreateInstance(动态创建实例) /// /// 动态创建实例 /// /// 目标类型 /// 类型 /// 传递给构造函数的参数 /// public static T CreateInstance(Type type, params object[] parameters) { return Conv.To(Activator.CreateInstance(type, parameters)); } /// /// 动态创建实例 /// /// 目标类型 /// 类名,包括命名空间,如果类型不处于当前执行程序集中,需要包含程序集名,范例:Test.Core.Test2,Test.Core /// 传递给构造函数的参数 /// public static T CreateInstance(string className, params object[] parameters) { Type type = Type.GetType(className) ?? Assembly.GetCallingAssembly().GetType(className); return CreateInstance(type, parameters); } #endregion #region GetAssembly(获取程序集) /// /// 获取程序集 /// /// 程序集名称 /// public static Assembly GetAssembly(string assemblyName) { return Assembly.Load(new AssemblyName(assemblyName)); } #endregion #region GetAssemblies(从目录获取所有程序集) /// /// 从目录获取所有程序集 /// /// 目录绝对路径 /// 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(); } #endregion #region GetAttribute(获取特性信息) /// /// 获取特性信息 /// /// 泛型特性 /// 元数据 /// public static TAttribute GetAttribute(MemberInfo memberInfo) where TAttribute : Attribute { return (TAttribute)memberInfo.GetCustomAttributes(typeof(TAttribute), false).FirstOrDefault(); } #endregion #region GetAttributes(获取特性信息数据) /// /// 获取特性信息数组 /// /// 泛型特性 /// 元数据 /// public static TAttribute[] GetAttributes(MemberInfo memberInfo) where TAttribute : Attribute { return Array.ConvertAll(memberInfo.GetCustomAttributes(typeof(TAttribute), false), x => (TAttribute)x); } #endregion #region IsBool(是否布尔类型) /// /// 是否布尔类型 /// /// 成员 /// public static bool IsBool(MemberInfo member) { if (member == null) { return false; } switch (member.MemberType) { case MemberTypes.TypeInfo: return member.ToString() == "System.Boolean"; case MemberTypes.Property: return IsBool((PropertyInfo)member); } return false; } /// /// 是否布尔类型 /// /// 属性 /// public static bool IsBool(PropertyInfo property) { return property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?); } #endregion #region IsEnum(是否枚举类型) /// /// 是否枚举类型 /// /// 成员 /// public static bool IsEnum(MemberInfo member) { if (member == null) { return false; } switch (member.MemberType) { case MemberTypes.TypeInfo: return ((TypeInfo)member).IsEnum; case MemberTypes.Property: return IsEnum((PropertyInfo)member); } return false; } /// /// 是否枚举类型 /// /// 属性 /// public static bool IsEnum(PropertyInfo property) { if (property.PropertyType.GetTypeInfo().IsEnum) { return true; } var value = Nullable.GetUnderlyingType(property.PropertyType); if (value == null) { return false; } return value.GetTypeInfo().IsEnum; } #endregion #region IsDate(是否日期类型) /// /// 是否日期类型 /// /// 成员 /// public static bool IsDate(MemberInfo member) { if (member == null) { return false; } switch (member.MemberType) { case MemberTypes.TypeInfo: return member.ToString() == "System.DateTime"; case MemberTypes.Property: return IsDate((PropertyInfo)member); } return false; } /// /// 是否日期类型 /// /// 属性 /// public static bool IsDate(PropertyInfo property) { if (property.PropertyType == typeof(DateTime)) { return true; } if (property.PropertyType == typeof(DateTime?)) { return true; } return false; } #endregion #region IsInt(是否整型) /// /// 是否整型 /// /// 成员 /// public static bool IsInt(MemberInfo member) { if (member == null) { return false; } switch (member.MemberType) { case MemberTypes.TypeInfo: return member.ToString() == "System.Int32" || member.ToString() == "System.Int16" || member.ToString() == "System.Int64"; case MemberTypes.Property: return IsInt((PropertyInfo)member); } return false; } /// /// 是否整型 /// /// 成员 /// public 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; } #endregion #region IsNumber(是否数值类型) /// /// 是否数值类型 /// /// 成员 /// public static bool IsNumber(MemberInfo member) { if (member == null) { return false; } if (IsInt(member)) { return true; } switch (member.MemberType) { case MemberTypes.TypeInfo: return member.ToString() == "System.Double" || member.ToString() == "System.Decimal" || member.ToString() == "System.Single"; case MemberTypes.Property: return IsNumber((PropertyInfo)member); } return false; } /// /// 是否数值类型 /// /// 属性 /// public 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; } #endregion #region IsGenericCollection(是否泛型集合) /// /// 是否泛型集合 /// /// 类型 /// public static bool IsGenericCollection(Type type) { if (!type.IsGenericType) { return false; } var typeDefinition = type.GetGenericTypeDefinition(); return typeDefinition == typeof(IEnumerable<>) || typeDefinition == typeof(IReadOnlyCollection<>) || typeDefinition == typeof(IReadOnlyList<>) || typeDefinition == typeof(ICollection<>) || typeDefinition == typeof(IList<>) || typeDefinition == typeof(List<>); } #endregion #region GetTypeName(获取类型名称) /// /// 减去全名正则 /// static readonly Regex SubtractFullNameRegex = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+", RegexOptions.Compiled); /// /// 获取类型名称 /// /// 类型 /// public static string GetTypeName(Type type) { return SubtractFullNameRegex.Replace(type.AssemblyQualifiedName, ""); } #endregion #region GetType(获取类型) /// /// 获取类型 /// /// 类型 /// public static Type GetType() { var type = typeof(T); return Nullable.GetUnderlyingType(type) ?? type; } #endregion } }