我可以强制32位AppDomain布局与64位AppDomain相同的对象吗? [英] Can I force a 32-bit AppDomain to layout objects the same as in a 64-bit AppDomain?

查看:97
本文介绍了我可以强制32位AppDomain布局与64位AppDomain相同的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现结构或类的字段(在托管内存中)的布局有所不同,具体取决于类型是在32位还是64位AppDomain上实例化的.

这些差异通常是由于对齐需要而填充的,并且似乎是硬编码的".在CLR中.

我想以某种方式防止这种情况,以便结构/类在32位和64位AppDomain上在物理上都是相同的-如果在32位和64位AppDomain之间传递的指针可以安全使用,它总是指向相同的类型.

有办法吗?有一些属性吗?什么?

注意:我不是在谈论封送数据-而是托管数据-因此FieldOffsetAttribute对此没有用.

谢谢

Cap'n

解决方案


I have discovered that a struct or class's fields are laid out (in managed memory) differently depending on whether the type is instantiated on a 32-bit or 64-bit AppDomain.

These differences are typically padding due to alignment needs and appear to be "hard coded" in the CLR.

I want to prevent this somehow so that a struct/class is physically identical on both 32-bit and 64-bit AppDomains - that is a pointer passed between a 32-bit and 64-bit AppDomain would be safe to use if it was always pointing to the same type.

Is there way? is there some attribute for this? anything?

Note: I'm not talking about marshaled data - but managed data - so the FieldOffsetAttribute is useless for this.

Thanks

Cap'n

解决方案

The [StructLayout] attribute, when marked Explicit, along with [FieldOffset], will actually work for managed types as well as marshalled data.

However, this doesn't give you complete control, as it only controls the layout from the first specified offset  (start of the object), and not the alignment of the object as a whole, etc.  Within the object, this will cause the memory layout to be identical - I've used this to do "unions" in C#, etc, and it works the same on 32bit and 64bit.


这篇关于我可以强制32位AppDomain布局与64位AppDomain相同的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆