1 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
只要解析的庫在同一文件夾下(或在 GAC 中),依賴關(guān)系就會自動解析 如果庫在特定文件夾下,自動解析可能會失敗,但您可以使用AppDomain.AssemblyResolve事件處理它。
此外,您似乎正在嘗試實(shí)現(xiàn)一種插件/插件主機(jī),也許您可以嘗試使用托管可擴(kuò)展性框架而不是通過反射手動實(shí)現(xiàn)解決方案。
編輯:遵循事件使用的代碼片段,但需要根據(jù)您的環(huán)境進(jìn)行調(diào)整
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 關(guān)注
- 161 瀏覽
添加回答
舉報(bào)