要以递归方式将所有引用加载到AppDomain的程序集,可以使用以下方法:
以下是一个示例代码:
public static void LoadAllReferencedAssemblies()
{
var loadedAssemblies = new HashSet<string>(AppDomain.CurrentDomain.GetAssemblies().Select(a => a.FullName));
var loadedAssemblyPaths = new HashSet<string>(AppDomain.CurrentDomain.GetAssemblies().Select(a => a.Location));
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
foreach (var path in referencedPaths)
{
var assemblyName = AssemblyName.GetAssemblyName(path);
if (!loadedAssemblies.Contains(assemblyName.FullName))
{
var assembly = Assembly.LoadFrom(path);
var referencedAssemblies = assembly.GetReferencedAssemblies();
foreach (var referencedAssembly in referencedAssemblies)
{
if (!loadedAssemblies.Contains(referencedAssembly.FullName))
{
var referencedAssemblyPath = Path.Combine(Path.GetDirectoryName(path), referencedAssembly.Name + ".dll");
if (File.Exists(referencedAssemblyPath))
{
LoadAllReferencedAssemblies(referencedAssemblyPath);
}
}
}
}
}
}
这个方法可以确保所有引用程序集都被加载到AppDomain中,从而可以确保所有类型都可以被正确地加载和使用。
领取专属 10元无门槛券
手把手带您无忧上云