using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
namespace DocumentServiceAPI.Utility
{
public class UtilityFun
{
///
/// 获取所有程序目录下和常用的程序集 包括一些系统引用程序集
///
/// 当前工程下的程序集
private static List? _allAssemblies = null;
public static List GetAllAssembly()
{
if (_allAssemblies == null)
{
_allAssemblies = new List();
string path = null;
string singlefile = null;
try
{
path = Assembly.GetEntryAssembly()?.Location;
}
catch { }
if (string.IsNullOrEmpty(path))
{
singlefile = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
path = Path.GetDirectoryName(singlefile);
}
var dir = new DirectoryInfo(Path.GetDirectoryName(path));
var dlls = dir.GetFiles("*.dll", SearchOption.TopDirectoryOnly);
string[] systemdll = new string[]
{
"Microsoft.",
"System.",
"Swashbuckle.",
"ICSharpCode",
"Newtonsoft.",
"Oracle.",
"Pomelo.",
"SQLitePCLRaw.",
"Aliyun.OSS",
"BouncyCastle.",
"FreeSql.",
"Google.Protobuf.dll",
"Humanizer.dll",
"IdleBus.dll",
"K4os.",
"MySql.Data.",
"Npgsql.",
"NPOI.",
"netstandard",
"MySqlConnector",
"VueCliMiddleware"
};
var filtered = dlls.Where(x => systemdll.Any(y => x.Name.StartsWith(y)) == false);
foreach (var dll in filtered)
{
try
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(dll.FullName);
}
catch
{
}
}
var dlllist = AssemblyLoadContext.Default.Assemblies.Where(x => systemdll.Any(y => x.FullName.StartsWith(y)) == false).ToList();
_allAssemblies.AddRange(dlllist);
}
return _allAssemblies;
}
}
}