gpt4 book ai didi

c# - 未找到引用程序集 - 如何获取解决方案中包含的所有 DLL

转载 作者:太空狗 更新时间:2023-10-29 22:12:35 25 4
gpt4 key购买 nike

我正在运行一个 WCF 应用程序 CoreApplication,它的 VS 项目引用了 AncillaryProjectCoreApplication 使用 AncillaryProject 中的 Provider 类;但是,它从未被显式引用 - 它是通过反射调用的。

我的问题是有时 CoreApplication 找不到Provider 因为AncillaryProject 没有出现在调用中到 GetAssemblies()。有时它工作正常,但有时(我猜它可能是在 JIT 之后)它失败了。

这是我的原始代码:

var providers = from d in AppDomain.CurrentDomain.GetAssemblies()
from c in d.GetTypes()
where typeof(BaseProvider).IsAssignableFrom(c)
select c;

看了this question之后,我尝试使用 GetReferencedAssemblies():

var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
allAssemblies = allAssemblies.Union(
a.GetReferencedAssemblies()
.Select(b => System.Reflection.Assembly.Load(b)));
}
var providers = from d in allAssemblies
from c in d.GetTypes()
where typeof(BaseProvider).IsAssignableFrom(c)
select c;

我意识到我引用的问题通过动态加载 bin 目录中的所有 dll 文件解决了问题,但这对我来说并不是特别好。 是否有更好的方法来执行此操作,或者 .NET 根本不加载其他程序集? 这在幕后是如何工作的,我能做些什么吗?

最佳答案

根据 Microsoft 文档 AppDomain.CurrentDomain.GetAssemblies() 获取已加载到此应用程序域的执行上下文中的程序集。 About AppDomain.CurrentDomain.GetAssemblies()

看来您需要更改加载所需程序集的策略,从使用 appdomain 到在应用程序文件夹中查找 dll。

我发现了一个类似问题的讨论 here

关于c# - 未找到引用程序集 - 如何获取解决方案中包含的所有 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9997508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com