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 關注
- 169 瀏覽
添加回答
舉報