.NET Framework x86 vs 64 vs anycpu

描述:

针对manage code c#, VS 编译时有x86, x64以及anycpu 的变异选项, 而针对unmanaged code c++,则没有anycpu的选项。

那么对于c# 代码,3中 编译条件究竟有什么差别呢:

x86: the assembly只会被作为x86的assembly 加载, 即使.NET framework 或者OS 是64位, 不能被x64 进程加载, 否则抛出BadImageFormatException

x64: assembly 只能作为x64 assembly 加载, x86 进程和os 无法加载。

anycpu: 编译所产生的assembly 不受cpu 类型影响, 如果assembly 类型位exe,  当在x64 os  .net framework上运行时,他就是x64的进程,反之则为x86进程。如果assembly 类型为dll 时,它的类型与加载它的进程类型相同,例如在64 os 系统中, 有一个x86进程加载了一个 anycpu 编译的dll, 此时加载的dll为32位的。

scenarios:

1. On 32 bit os:

a. x86.exe ---- run as a 32 bit process, can load assemblies that compiled by anycpu and x86, will get BadImageFormatException when loads x64 compiled assembly.(dll)

b. x64.exe ---  can not be run and get BadImageFormatException error.

c. anycpu.exe --- same as x86.exe


2. On 64 bit os:

a. x86.exe ---- run as a 32 bit process, can load assemblies that compiled by anycpu and x86, will get BadImageFormatException when loads x64 compiled assembly.(dll)

b. x64.exe ---- run as a 64 bit process, assemblies compiled by anycpu and x64 can be loaded, will get BadImageFormatException when try to load x86 assembly.(dll)

c. anycpu.exe --- same as x64.exe.


PS:

如果exe 在x64 os中运行,且exe 依赖32位的assembly, 那么该exe必须以x86 编译, 否则在64位系统中,用anycpu编译出来的会座位64 bit exe 运行,这样会导致在加载32 bit dependance dll 时 出现异常。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值