1 回答
TA贡献1804条经验 获得超8个赞
只要解析的库在同一文件夹下(或在 GAC 中),依赖关系就会自动解析 如果库在特定文件夹下,自动解析可能会失败,但您可以使用AppDomain.AssemblyResolve事件处理它。
此外,您似乎正在尝试实现一种插件/插件主机,也许您可以尝试使用托管可扩展性框架而不是通过反射手动实现解决方案。
编辑:遵循事件使用的代码片段,但需要根据您的环境进行调整
static Injector()
{
// Usage of static constructor because we need a unique static handler
// But feel free to move this part to a more global location
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string foundAssemblyPath = string.Empty;
// args.Name contains name of the missing assembly
// some project-specific and environment-specific code should be added here to manually resolve the dependant library
// In this example I will consider that the libraries are located under /plugins folder
foundAssemblyPath = $@"{Path.GetDirectoryName(Application.StartupPath)}\plugins\{args.Name}.dll";
return Assembly.LoadFile(foundAssemblyPath);
}
- 1 回答
- 0 关注
- 188 浏览
添加回答
举报
