最核心方法是使用AppDomain.CurrentDomain.AssemblyResolve事件,在CLR无法找到程序集时介入,通过自定义逻辑加载程序集,适用于插件架构、版本冲突、嵌入式程序集等场景,需注意性能、缓存、加载上下文及错误处理等最佳实践。

要自定义.NET程序集解析,最核心且常用的方法是利用
AppDomain.CurrentDomain.AssemblyResolve
当.NET运行时无法找到它需要的某个程序集时,例如,你引用的一个DLL不在应用程序的基目录或配置的探测路径中,或者存在版本冲突,
AppDomain.CurrentDomain.AssemblyResolve
具体来说,你需要为
AppDomain.CurrentDomain.AssemblyResolve
ResolveEventArgs
Name
Assembly.Load
Assembly.LoadFrom
null
下面是一个简单的C#代码示例,演示如何从应用程序的一个特定子文件夹中加载程序集:
using System;
using System.IO;
using System.Reflection;
public class CustomAssemblyResolver
{
public static void Initialize()
{
// 注册AssemblyResolve事件处理器
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Console.WriteLine("AssemblyResolve事件已注册。");
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine($"尝试解析程序集: {args.Name}");
// 解析程序集名称,通常是 "AssemblyName, Version=X.X.X.X, Culture=neutral, PublicKeyToken=..."
string assemblyName = new AssemblyName(args.Name).Name;
// 构建可能的程序集路径。这里假设程序集在 "Plugins" 子文件夹中。
// 你可以根据自己的逻辑来构建路径,比如从配置中读取。
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string pluginsPath = Path.Combine(baseDirectory, "Plugins");
string assemblyFilePath = Path.Combine(pluginsPath, assemblyName + ".dll");
Console.WriteLine($"尝试从路径加载: {assemblyFilePath}");
if (File.Exists(assemblyFilePath))
{
try
{
// 使用Assembly.LoadFrom加载程序集。
// 注意:LoadFrom有其特定的加载上下文行为,有时Assembly.LoadFile可能更合适,取决于具体场景。
Assembly loadedAssembly = Assembly.LoadFrom(assemblyFilePath);
Console.WriteLine($"成功加载程序集: {loadedAssembly.FullName}");
return loadedAssembly;
}
catch (Exception ex)
{
Console.Error.WriteLine($"加载程序集 {assemblyFilePath} 失败: {ex.Message}");
// 记录错误,并返回null,让CLR尝试其他方式或报错
return null;
}
}
Console.WriteLine($"未能在自定义路径找到程序集: {assemblyName}");
// 如果我们没有找到,返回null,CLR会继续其默认的解析流程或抛出FileNotFoundException
return null;
}
// 示例用法
public static void Main(string[] args)
{
Initialize();
// 模拟一个会失败的程序集加载,如果Plugins文件夹里没有这个DLL
// 比如,你有一个项目引用了一个名为 "MyPlugin.dll" 的程序集,
// 但它只放在了应用程序的Plugins子文件夹中。
try
{
// 假设MyPlugin有一个类型叫做MyPlugin.MyClass
// Type pluginType = Type.GetType("MyPlugin.MyClass, MyPlugin");
// if (pluginType == null)
// {
// Console.WriteLine("MyPlugin.MyClass 类型未能找到,但AssemblyResolve可能已经介入。");
// }
// else
// {
// Console.WriteLine("MyPlugin.MyClass 类型成功找到。");
// }
// 更直接的测试方式是尝试加载一个不存在于标准路径的DLL
// 假设我们有一个名为 "NonStandardAssembly" 的DLL在 "Plugins" 文件夹
Assembly nonStandardAssembly = Assembly.Load("NonStandardAssembly");
Console.WriteLine($"非标准程序集 {nonStandardAssembly.FullName} 已加载。");
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"程序集加载失败(FileNotFoundException):{ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"发生其他错误:{ex.Message}");
}
Console.ReadKey();
}
}这段代码的核心在于
CurrentDomain_AssemblyResolve
ResolveEventArgs
Plugins
Assembly.LoadFrom
Assembly.LoadFrom
null
说实话,自定义程序集解析通常不是我们优先考虑的方案,它更像是一个“备用轮胎”或者“特种工具”。当.NET运行时默认的程序集探测机制无法满足需求时,
AssemblyResolve
常见的应用场景包括:
Plugins
AssemblyResolve
Newtonsoft.Json v12
Newtonsoft.Json v13
AssemblyResolve
AssemblyResolve
Assembly.Load(byte[])
AssemblyResolve
AssemblyResolve
在我看来,这些场景的核心都是“非标准”二字。当你的程序集不在CLR预期的位置,或者你需要对加载过程进行深度干预时,
AssemblyResolve
虽然
AssemblyResolve
常见的陷阱:
AssemblyResolve
AssemblyResolve
AssemblyResolve
Assembly.LoadFrom
AssemblyResolve
最佳实践:
ResolveEventArgs.Name
null
AssemblyResolve
null
FileNotFoundException
BadImageFormatException
null
AssemblyResolve
Assembly.LoadFile
Assembly.Load(byte[])
AppDomain
Assembly.LoadFile
Assembly.Load(byte[])
Assembly.LoadFrom
LoadFile
总的来说,处理
AssemblyResolve
在.NET中,程序集加载是一个相当复杂但又非常灵活的机制,
AssemblyResolve
主要的程序集加载机制和事件:
GAC(Global Assembly Cache - 全局程序集缓存)
AssemblyResolve
AssemblyResolve
应用程序基目录和探测路径(Probing Paths)
AppDomain.BaseDirectory
app.config
<probing privatePath="..."/>
AssemblyResolve
AssemblyResolve
Assembly.Load()
Assembly.Load(string assemblyName)
Assembly.LoadFrom(string path)
Assembly.LoadFile(string path)
Assembly.Load(byte[] rawAssembly)
AssemblyResolve
Load
AssemblyResolve
AppDomain.CurrentDomain.AssemblyLoad
AssemblyResolve
AssemblyLoad
AppDomain
AssemblyResolve
AssemblyResolve
AssemblyLoad
AssemblyResolve
AssemblyLoad
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve
Assembly.ReflectionOnlyLoad()
Assembly.ReflectionOnlyLoadFrom()
AssemblyResolve
AssemblyResolve
AppDomain.CurrentDomain.TypeResolve
ResourceResolve
TypeResolve
ResourceResolve
AssemblyResolve
AssemblyResolve
AssemblyResolve
TypeResolve
ResourceResolve
总结异同:
Assembly.Load
AssemblyResolve
AssemblyLoad
Assembly.Load
AssemblyResolve
AssemblyLoad
AssemblyResolve
TypeResolve
ResourceResolve
在我看来,
AssemblyResolve
以上就是.NET的AssemblyResolution事件如何自定义程序集解析?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号