• .net core 程序集帮助类


     public class RuntimeHelper
        {
            /// <summary>
            /// 获取项目程序集,排除所有的系统程序集(Microsoft.***、System.***等)、Nuget下载包
            /// </summary>
            /// <returns></returns>
            public static IList<Assembly> GetAllAssemblies()
            {
                List<Assembly> list = new List<Assembly>();
                var deps = DependencyContext.Default;
                //排除所有的系统程序集、Nuget下载包
                var libs = deps.CompileLibraries.Where(lib => !lib.Serviceable && lib.Type != "package");
                foreach (var lib in libs)
                {
                    try
                    {
                        var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name));
                        list.Add(assembly);
                    }
                    catch (Exception ex)
                    {
                        //
                    }
                }
                return list;
            }
    
            public static Assembly GetAssembly(string assemblyName)
            {
                return GetAllAssemblies().FirstOrDefault(f => f.FullName.Contains(assemblyName));
            }
    
            public static IList<Type> GetAllTypes()
            {
                List<Type> list = new List<Type>();
                foreach (var assembly in GetAllAssemblies())
                {
                    var typeinfos = assembly.DefinedTypes;
                    foreach (var typeinfo in typeinfos)
                    {
                        list.Add(typeinfo.AsType());
                    }
                }
                return list;
            }
    
            /// <summary>
            /// 根据AssemblyName获取所有的类
            /// </summary>
            /// <param name="assemblyName"></param>
            /// <returns></returns>
            public static IList<Type> GetTypesByAssembly(string assemblyName)
            {
                List<Type> list = new List<Type>();
                var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
                var typeinfos = assembly.DefinedTypes;
                foreach (var typeinfo in typeinfos)
                {
                    list.Add(typeinfo.AsType());
                }
                return list;
            }
    
            public static Type GetImplementType(string typeName, Type baseInterfaceType)
            {
                return GetAllTypes().FirstOrDefault(t =>
                {
                    if (t.Name == typeName && t.GetTypeInfo().GetInterfaces().Any(b => b.Name == baseInterfaceType.Name))
                    {
                        var typeinfo = t.GetTypeInfo();
                        return typeinfo.IsClass && !typeinfo.IsAbstract && !typeinfo.IsGenericType;
                    }
                    return false;
                });
            }
        }
  • 相关阅读:
    vim快速查找
    一次特别二不兮兮的WebStorm经历
    让docker容器使用主机系统时间(挂入/etc/localtime)
    systemd:在service文件中给Exec传入多个参数
    mongodb数据迁移
    明日边缘;逃出克隆岛
    [C++] 类的所有对象实例共享静态类成员变量
    HTTP长连接
    fqPkzJetPK
    何时使用move
  • 原文地址:https://www.cnblogs.com/li-lun/p/9618238.html
Copyright © 2020-2023  润新知