|
|
@@ -0,0 +1,173 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
+using System.Text;
|
|
|
+using WCS.Core.BaseExtensions;
|
|
|
+
|
|
|
+namespace WCS.Core.Helpers
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// 反射辅助类
|
|
|
+ /// </summary>
|
|
|
+ public class ReflectionHelper
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 创建指定类型的实例
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TModel"></typeparam>
|
|
|
+ /// <param name="type"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static TModel CreateInstance<TModel>(Type type) => (TModel)ReflectionHelper.CreateInstance(type);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 创建指定类型的实例
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="type"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static object CreateInstance(Type type) => Activator.CreateInstance(type);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从当前程序中 获取所有实现指定接口的类型
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TInterface"></typeparam>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Type> GetTypesByImplementInterface<TInterface>() where TInterface : class => GetAssemblies().SelectMany(GetTypesByImplementInterface<TInterface>).ToList();
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从指定的程序集里 获取所有实现指定接口的类型
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TInterface"></typeparam>
|
|
|
+ /// <param name="assembly"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Type> GetTypesByImplementInterface<TInterface>(Assembly assembly) where TInterface : class
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return assembly.GetTypes() //获取该程序集中所有的类
|
|
|
+ .Where(type => !string.IsNullOrWhiteSpace(type.Namespace))//命名空间不为空
|
|
|
+ .Where(type => type.GetTypeInfo().IsClass) //必须要有类
|
|
|
+ .Where(type => type.GetTypeInfo().BaseType != null)//基本类型不为空
|
|
|
+ .Where(type => !type.GetTypeInfo().IsAbstract) //没有作废弃用
|
|
|
+ // IsAssignableFrom
|
|
|
+ // 方法的作用
|
|
|
+ // a对象所对应类信息是b对象所对应的类信息的父类或者是父接口,简单理解即a是b的父类或接口 a对象所对应类信息与b对象所对应的类信息相同,简单理解即a和b为同一个类或同一个接口
|
|
|
+ .Where(type => typeof(TInterface).IsAssignableFrom(type))// 当前类是否继承或指定接口
|
|
|
+ .ToList();
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return new List<Type>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从指定的程序集里 获取所有实现指定接口的类型
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TInterface"></typeparam>
|
|
|
+ /// <param name="assemblies"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Type> GetTypesByImplementInterface<TInterface>(List<Assembly> assemblies) where TInterface : class
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ List<Type> types = new List<Type>();
|
|
|
+ assemblies.ForEach(p =>
|
|
|
+ {
|
|
|
+ types.AddRange(p.GetTypes() //获取该程序集中所有的类
|
|
|
+ .Where(type => !string.IsNullOrWhiteSpace(type.Namespace))//命名空间不为空
|
|
|
+ .Where(type => type.GetTypeInfo().IsClass) //必须要有类
|
|
|
+ .Where(type => type.GetTypeInfo().BaseType != null)//基本类型不为空
|
|
|
+ .Where(type => !type.GetTypeInfo().IsAbstract) //没有作废弃用
|
|
|
+ // IsAssignableFrom
|
|
|
+ // 方法的作用
|
|
|
+ // a对象所对应类信息是b对象所对应的类信息的父类或者是父接口,简单理解即a是b的父类或接口 a对象所对应类信息与b对象所对应的类信息相同,简单理解即a和b为同一个类或同一个接口
|
|
|
+ .Where(type => typeof(TInterface).IsAssignableFrom(type))// 当前类是否继承或指定接口
|
|
|
+ .ToList());
|
|
|
+ });
|
|
|
+
|
|
|
+ return types;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return new List<Type>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取项目中所有程序集 (注意,排除了一些指定的程序集)
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>AppContext.BaseDirectory 方法的作用是获取程序所在目录</remarks>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Assembly> GetAssemblies()
|
|
|
+ {
|
|
|
+ var assemblies = new List<Assembly>();
|
|
|
+ foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
|
|
+ {
|
|
|
+ assembly.GetReferencedAssemblies().RemoveTheAssemblyNamespecified().ToList()
|
|
|
+ .ForEach(p =>
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ assemblies.Add(Assembly.Load(p));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Log(" 警告! 加载程序集失败:" + p.FullName);
|
|
|
+ Log(" 错误: " + ex.Message);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ assemblies.AddRange(GetAssemblies(AppContext.BaseDirectory));
|
|
|
+ return assemblies.RemoveDuplicateAssemblies().ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从指定的目录开始,提取所有可用的程序集,向上查找引用的
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Assembly> GetAssemblies(string path)
|
|
|
+ {
|
|
|
+ var source = new List<Assembly>(); //需要返回的数据集合
|
|
|
+ foreach (var file in Directory.GetFiles(path, "*.dll"))//读取指定目录中所有的DLL文件路径,并遍历
|
|
|
+ {
|
|
|
+ //try
|
|
|
+ //{
|
|
|
+ var a = Assembly.GetEntryAssembly();
|
|
|
+ var assembly = Assembly.LoadFrom(file); //将获取到的程序集加载出来
|
|
|
+ source.Add(assembly); //将该程序集加入到需要返回的数据中
|
|
|
+ //找到所有引用过这个程序集的程序集的名称,并进行遍历
|
|
|
+ assembly.GetReferencedAssemblies().RemoveTheAssemblyNamespecified().ToList()
|
|
|
+ .ForEach(p =>
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ source.Add(Assembly.Load(p));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Log(" 警告! 加载程序集失败:" + p.FullName);
|
|
|
+ Log(" 错误: " + ex.Message);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //}
|
|
|
+ //catch (Exception ex)
|
|
|
+ //{
|
|
|
+ // Log(" 警告! 提取程序集失败:" + file);
|
|
|
+ // Log(" 错误: " + ex.Message);
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ return source.DistinctBy(p => p.FullName).ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 写日志
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="_">日志内容</param>
|
|
|
+ private static void Log(string _)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|