简体   繁体   English

如何运行时链接“插件”参考DLL

[英]How to runtime link “plugin” reference dlls

I am afraid this is a too broad question. 恐怕这是一个太广泛的问题。 But I still have not a clue as to how start this job so even a clue would help since nothing came out from googling... 但是我对如何开始这项工作仍然一无所知,因此即使是一个线索也无济于事,因为谷歌搜索没有任何结果。

Whenever I link a reference I add it before building it. 每当我链接参考时,我都会在构建参考之前将其添加。 What I'd need is to be able to add whatever reference is added in a dir. 我需要的是能够添加在目录中添加的任何引用。 That would allow me to put a dll in a dir and add it AT RUNTIME ONLY if that dll is present. 那将允许我将一个dll放在一个目录中,并仅在存在该dll的情况下才在运行时添加它。

So say 所以说

  • that nothing is added at start ---> no additional features. 开始时什么都没添加--->没有其他功能。
  • I add 1.dll ----> the features present in 1.dll shall be added 我添加1.dll ---->应该添加1.dll中存在的功能
  • I add 2.dll ----> the features present in 2.dll shall be added 我添加了2.dll ---->应该添加2.dll中存在的功能

and so on... 等等...

I call those dlls plugin since they are similar to plugins. 我将这些dll插件称为“插件”,因为它们类似于插件。

Thank you in advance for your help. 预先感谢您的帮助。 Patrick 帕特里克

You could create a new AppDomain and set the appRelativeSearchPath to your plugin folder. 您可以创建一个新的AppDomain并将appRelativeSearchPath设置到您的插件文件夹。 Then you are able to load the additional assemblies using the Load method of your new AppDomain. 然后,您可以使用新AppDomain的Load方法加载其他程序集。

If you want to access classes from the new AppDomain in your applications main AppDomain you can instantiate them using the CreateInstanceAndUnwrap method of AppDomain. 如果要从应用程序主AppDomain中的新AppDomain访问类,则可以使用AppDomain的CreateInstanceAndUnwrap方法实例化它们。 But the classes in your plugin assemblies have to inherit MarshalRefObject for this to work. 但是插件程序集中的类必须继承MarshalRefObject才能起作用。

I hope this points you into the right direction. 我希望这可以为您指明正确的方向。

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

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