using System.Reflection; using System.Runtime.Loader; using System.IO; using Microsoft.Extensions.DependencyModel; namespace DS.Module.Core.Reflection; public static class AssemblyHelper { private static Assembly[] GetAllAssemblies() { string[] filters = { "mscorlib", "netstandard", "dotnet", "api-ms-win-core", "runtime.", "System", "Microsoft", "Window", }; DependencyContext context = DependencyContext.Default; List names = new List(); foreach (CompilationLibrary library in context.CompileLibraries) { string name = library.Name; if (filters.Any(name.StartsWith)) { continue; } if (!names.Contains(name)) { names.Add(name); } } return LoadFiles(names); } private static Assembly[] LoadFiles(IEnumerable files) { List assemblies = new List(); foreach (string file in files) { AssemblyName name = new AssemblyName(file); try { assemblies.Add(Assembly.Load(name)); } catch (FileNotFoundException) { } } return assemblies.ToArray(); } public static Assembly[] FindAllItems() { return GetAllAssemblies().ToArray(); } /// /// 根据程序集名字得到程序集 /// /// /// public static IEnumerable GetAssembliesByName(params string[] assemblyNames) { var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath; //获取项目路径 return assemblyNames.Select(o => AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(basePath, $"{o}.dll"))); } }