You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using Microsoft.Extensions.DependencyModel;
|
|
using System.Reflection;
|
|
using System.Runtime.Loader;
|
|
|
|
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<string> names = new List<string>();
|
|
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<string> files)
|
|
{
|
|
List<Assembly> assemblies = new List<Assembly>();
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据程序集名字得到程序集
|
|
/// </summary>
|
|
/// <param name="assemblyNames"></param>
|
|
/// <returns></returns>
|
|
|
|
public static IEnumerable<Assembly> GetAssembliesByName(params string[] assemblyNames)
|
|
{
|
|
var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath; //获取项目路径
|
|
return assemblyNames.Select(o => AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(basePath, $"{o}.dll")));
|
|
}
|
|
} |