简体   繁体   English

如何在另一个参考程序集中加载一个参考程序集?

[英]How to load a referenced assembly within another referenced assembly?

I have a scenario where my client is a WPF App let's say W , There are 3 assemblies say A , B , C which are not to be exposed to W , hence the 3 assemblies are referenced in a concrete factory , say F which creates the required instance and provides to W . 我有一个方案,我的客户是WPF应用程序,假设W ,有三个程序集ABC不会暴露给W ,因此这3个程序集在一个具体工厂中引用,例如F ,它创建了必需实例,并提供给W

I am using reflection in F to generate the 3 objects. 我在F使用反射来生成3个对象。

Below is the code 下面是代码

return (IClass)Activator.CreateInstance(Assembly.Load("A").GetType("A.AClass"), 
BindingFlags.NonPublic | BindingFlags.Instance, null, null, null);

The code works if I reference A in W , but doesn't work ( System.IO.FileNotFound Exception ) when A is referenced in F (I cross checked with Assembly.GetExecutingAssembly().GetReferencedAssemblies() and A is not loaded). 代码工作,如果我引用AW ,但不工作( System.IO.FileNotFound Exception ),当A被引用了F (我用交叉检查Assembly.GetExecutingAssembly().GetReferencedAssemblies()A未加载)。

I read that an Assembly may not be loaded if it is not required. 我读了一个Assembly ,如果不需要它可能不会被加载。 But how does my WPF client then load the assembly. 但是我的WPF客户端如何加载程序集。 If this is the expected behavior , I would go with finding the current Directory path and appending A.dll to it. 如果这是预期的行为,我将继续查找当前的Directory路径,并将A.dll附加到该路径。 Any thoughts around ? 有什么想法吗?

I suspect the files are not getting copied to the AppDomain.CurrentDomain.BaseDirectory when the solution is built. 我怀疑在构建解决方案时,文件没有被复制到AppDomain.CurrentDomain.BaseDirectory Without a reference you would need to manually ensure the required DLLs are being copied to the right location. 如果没有参考,则需要手动确保将所需的DLL复制到正确的位置。

See BasconSah comment above for a suggestion. 请参阅上面的BasconSah评论以获取建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM