简体   繁体   English

如何从动态位置将C#dll加载到C#dll项目中?

[英]How can I load a C# dll into a C# dll Project from a dynamic location?

The Program ClosedSourceProgram starts and copies my files into an unknown location. 程序ClosedSourceProgram启动,并将我的文件复制到未知位置。 Their Structure will be: 它们的结构将是:

?\project.dll
?\project\usefultools.dll

I have to load the usefultools.dll into my project but i dont know how i can tell C#VS2010 that the dll location will be 我必须将有用的tools.dll加载到我的项目中,但是我不知道如何告诉C#VS2010 dll的位置

project\usefultools.dll

I added the library to my assembly as a reference so that its content is known during developement - therefor it tried to load earlier than i could load it via Assembly.LoadFrom() . 我将该库添加到我的程序集中作为参考,以便在开发过程中知道它的内容-因此,它试图比我可以通过Assembly.LoadFrom()加载的时间早。

I manually copied the file for now into the ClosedSourceProgram-root directory. 我现在手动将文件复制到ClosedSourceProgram-root目录。 It now loads without Assembly.LoadFrom() . 现在可以不使用Assembly.LoadFrom()加载。

current working tree: 当前的工作树:

?\ClosedSourceProgram\ClosedSourceProgram.exe
?\ClosedSourceProgram\usefultools.dll
?\ClosedSourceProgram\temp\project.dll

the tree i want: 我想要的树:

?\ClosedSourceProgram\ClosedSourceProgram.exe
?\ClosedSourceProgram\temp\project.dll
?\ClosedSourceProgram\temp\project\usefultools.dll

I would like to tell my project that it should search subdirectories if it cant find the dll in the root directory - how can i do that? 我想告诉我的项目,如果它不能在根目录中找到dll,则应该搜索子目录-我该怎么做?

You can load an assembly from an arbitrary location at runtime using Assembly.LoadFrom , and passing a filename. 您可以在运行时使用Assembly.LoadFrom从任意位置加载程序集,并传递文件名。


Edit: 编辑:

Another option is to subscribe to the AppDomain.AssemblyResolve event, and load the assemblies on demand. 另一个选择是订阅AppDomain.AssemblyResolve事件,并按需加载程序集。 This has the advantage of allowing for any structure, since all undiscovered assemblies can loaded as needed by you. 这具有允许使用任何结构的优点,因为所有未发现的程序集都可以根据您的需要进行加载。

您可以随时尝试Assembly.LoadFrom()

You can use the "~" operator if the assembly is with in any of the subfolders of the application path. 如果程序集位于应用程序路径的任何子文件夹中,则可以使用“〜”运算符。

Assembly.LoadFrom("~\\project\\usefultools.dll"); Assembly.LoadFrom( “〜\\项目\\ usefultools.dll”);

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

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