简体   繁体   English

如何动态加载项目外的DLL?

[英]How do I dynamically load dll outside project?

How can I dynamically load a DLL? 如何动态加载DLL?

Please don't suggest: 不要建议:

  1. Don't suggest GAC, because the DLL is already made and can't be changed or updated. 不建议GAC,因为DLL已经制作,无法更改或更新。
  2. Don't suggest hintpath, Assembly.LoadFrom(...) since this functionality works at debugging not outside the project. 不建议使用hintpath, Assembly.LoadFrom(...)因为此功能在调试时不在项目之外。

If I copy that DLL/exe onto a test machine outside the project then it shows an error: could not load .dll. 如果我将该DLL / exe复制到项目外的测试机器上,则会显示错误:无法加载.dll。

I also asked a question at the MSDN forums . 我还在MSDN论坛上提出了一个问题。

Could anyone help me? 谁能帮助我?

Thanks 谢谢
Ashish 阿希什

System.Reflection.Assembly.LoadFile() works just fine for this. System.Reflection.Assembly.LoadFile()适用于此。

If you are getting BadImageFormatException , check whether the assembly you are trying to load is compiled for the same platform target (x86 or x64) as the assembly doing the loading. 如果您收到BadImageFormatException ,请检查您尝试加载的程序集是否针对与执行加载的程序集相同的平台目标(x86或x64)进行编译。

Update based on comments 根据评论更新

Sounds like you are trying to load a .NET 2.0 assembly into a .NET 4.0 application, and you have already put the following in your application configuration file: 听起来您正在尝试将.NET 2.0程序集加载到.NET 4.0应用程序中,并且您已将以下内容放入应用程序配置文件中:

<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" />    
    </startup>
</configuration>

In that case, check to make sure your configuration file is getting deployed on your target machine alongside your executable. 在这种情况下,请检查以确保您的配置文件与可执行文件一起部署在目标计算机上。 For example, if your application is called MyProgram.exe , your configuration file should be called MyProgram.exe.config - and it needs to be copied to the same folder as the executable on your target machine to have any effect - the framework will load this file when it starts up, if it exists. 例如,如果您的应用程序名为MyProgram.exe ,则应将配置文件称为MyProgram.exe.config - 并且需要将其复制到与目标计算机上的可执行文件相同的文件夹才能生效 - 框架将加载该文件启动时是否存在。

1) Try using assembly.loadfile function instead of loadfrom. 1)尝试使用assembly.loadfile函数而不是loadfrom。 It will work outside the project also. 它也可以在项目之外工作。 2) If u got assembly mixed mode error then, just need to copy xml file along with dll generated in bin folder.xml file contains app.config file data. 2)如果你得到汇编混合模式错误那么,只需要复制xml文件和bin folder.xml文件中生成的dll包含app.config文件数据。 Code in app.config file is... app.config文件中的代码是......

also check link... http://social.msdn.microsoft.com/Forums/en/csharpide/thread/99691cc4-27df-48e7-b4aa-377f74109425 还查看链接... http://social.msdn.microsoft.com/Forums/en/csharpide/thread/99691cc4-27df-48e7-b4aa-377f74109425

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

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