[英]C# projects using both X86 and Any CPU
Say I have 2 Winform projects A, B. 假设我有2个Winform项目A,B。
Project A (target .NET 2.0) has to be run on x86 (it is an external library) and for legacy reasons project B (target .NET 4.0) has to be run on Any CPU. 项目A(目标.NET 2.0)必须在x86(它是一个外部库)上运行,并且由于遗留原因,项目B(目标.NET 4.0)必须在任何CPU上运行。 Now I need to call A's methods from project B but it throws an error: 现在我需要从项目B调用A的方法,但它会抛出一个错误:
"An unhandled exception of type 'System.BadImageFormatException' occurred in System.Windows.Forms.dll"
Additional information: Could not load file or assembly 'CSharpDemo, Version=1.0.5414.18636, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."
However, I found if I target project B on .NET 4.5 then this problem doesn't occur. 但是,我发现如果我在.NET 4.5上定位项目B,那么这个问题就不会发生了。 But I still want Project B to be targeting 4.0, how to achieve this? 但是我仍然希望B项目定位到4.0,如何实现这一目标?
If any of your code calls 32-bit x86 libraries, the entire process needs to load as x86. 如果您的任何代码调用32位x86库,则整个过程需要加载为x86。 No exceptions. 没有例外。
To do this, set the EXE project to x86, set DLL projects that have native x86 dependencies to x86, and for pure managed DLL projects you can use AnyCPU -- .NET will load them as x86 for your x86 EXE, but they can also load into x64 EXEs. 为此,将EXE项目设置为x86,将具有本机x86依赖项的DLL项目设置为x86,对于纯托管DLL项目,可以使用AnyCPU - .NET将它们作为x86加载到x86 EXE,但它们也可以加载到x64 EXE。
Remember that x86 code runs just fine on x64 processors running x64 Windows, with the help of the WOW64 compatibility layer. 请记住,在WOW64兼容层的帮助下,x86代码在运行x64 Windows的x64处理器上运行得很好。
(with limited exceptions: on Windows Server edition, WOW64 is an optional component, and in the preinstall environment there is no WOW64). (除了有限的例外:在Windows Server版本上,WOW64是一个可选组件,在预安装环境中没有WOW64)。
You may find the answer to your question in following blog post 您可以在以下博文中找到问题的答案
http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/ http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/
Here is summary 这是摘要
.net 4.0 .net 4.0
What AnyCPU used to mean up to .NET 4.0 (and Visual Studio 2010) is the following: AnyCPU用于表示.NET 4.0(和Visual Studio 2010)的含义如下:
•If the process runs on a 32-bit Windows system, it runs as a 32-bit process. •如果进程在32位Windows系统上运行,则它将以32位进程运行。 IL is compiled to x86 machine code. IL被编译为x86机器代码。
•If the process runs on a 64-bit Windows system, it runs as a 64-bit process. •如果进程在64位Windows系统上运行,则它将以64位进程运行。 IL is compiled to x64 machine code. IL编译为x64机器代码。
•If the process runs on an Itanium Windows system (has anyone got one? ;-) ), it runs as a 64-bit process. •如果进程在Itanium Windows系统上运行(任何人都有一个?;-)),它将作为64位进程运行。 IL is compiled to Itanium machine code. IL被编译为Itanium机器代码。
.net 4.5 .net 4.5
In .NET 4.5 and Visual Studio 11 the cheese has been moved. 在.NET 4.5和Visual Studio 11中,奶酪已被移动。 The default for most .NET projects is again AnyCPU, but there is more than one meaning to AnyCPU now. 大多数.NET项目的默认设置是AnyCPU,但现在AnyCPU有多个含义。 There is an additional sub-type of AnyCPU, “Any CPU 32-bit preferred”, which is the new default (overall, there are now five options for the /platform C# compiler switch: x86, Itanium, x64, anycpu, and anycpu32bitpreferred). 还有一个额外的子类型AnyCPU,“任何CPU 32位首选”,这是新的默认值(总体而言,/ platform C#编译器交换机现在有五个选项:x86,Itanium,x64,anycpu和anycpu32bitpreferred )。 When using that flavor of AnyCPU, the semantics are the following: 使用AnyCPU的那种风格时,语义如下:
•If the process runs on a 32-bit Windows system, it runs as a 32-bit process. •如果进程在32位Windows系统上运行,则它将以32位进程运行。 IL is compiled to x86 machine code. IL被编译为x86机器代码。
•If the process runs on a 64-bit Windows system, it runs as a 32-bit process. •如果进程在64位Windows系统上运行,则它将以32位进程运行。 IL is compiled to x86 machine code. IL被编译为x86机器代码。
•If the process runs on an ARM Windows system, it runs as a 32-bit process. •如果进程在ARM Windows系统上运行,则它将以32位进程运行。 IL is compiled to ARM machine code. IL编译为ARM机器代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.