简体   繁体   English

Assembly.LoadFrom 有效,Assembly.Load 无效

[英]Assembly.LoadFrom works, Assembly.Load does not

I am trying to load the file using Assembly.Load but I get a BadImageException, but if I try and load the exact same assembly using Assembly.LoadFrom it works.我正在尝试使用Assembly.Load加载文件,但我得到一个 BadImageException,但如果我尝试使用Assembly.LoadFrom加载完全相同的程序集,它就可以工作。

// this works.
var asm1 = Assembly.LoadFrom(path);

// this throws an exception
var ba = File.ReadAllBytes(path);
var asm2 = Assembly.Load(ba);

As you can see from the code above, the assembly is exactly the same, but it works for Assembly.LoadFrom but not Assembly.Load从上面的代码可以看出,程序集完全一样,但它适用于Assembly.LoadFrom而不是Assembly.Load

What could be the reason for it?可能是什么原因? How can I get Assembly.Load to work?我怎样才能让Assembly.Load工作?

Assmebly.Load might throw BadImageException while reflecting on C++ executable files might, unlike Assembly.LoadFrom(string) .Assembly.LoadFrom(string)不同,Assmebly.Load 在反映Assmebly.Load可执行文件时可能会抛出BadImageException For fixing that issue you have to use /fixed:no while linking.要解决该问题,您必须在链接时使用/fixed:no Here is the paragraph from Msdn:这是Msdn的一段:

Reflecting on C++ executable files might throw a BadImageFormatException.考虑 C++ 可执行文件可能会引发 BadImageFormatException。 This is most likely caused by the C++ compiler stripping the relocation addresses or the.reloc section from your executable file.这很可能是由 C++ 编译器从可执行文件中剥离重定位地址或 .reloc 部分引起的。 To preserve the.reloc address for your C++ executable file, specify /fixed:no when you are linking.要保留 C++ 可执行文件的 .reloc 地址,请在链接时指定 /fixed:no。

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

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