如果您想查看实际的异常,您将需要一台 64 位机器。我创建了一些重现问题的虚拟类。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class InnerType
{
char make;
char model;
UInt16 series;
}
[StructLayout(LayoutKind.Explicit)]
public class OutterType
{
[FieldOffset(0)]
char blah;
[FieldOffset(1)]
char blah2;
[FieldOffset(2)]
UInt16 blah3;
[FieldOffset(4)]
InnerType details;
}
class Program
{
static void Main(string[] args)
{
var t = new OutterType();
Console.ReadLine();
}
}
如果我在 64 clr 上运行它,我会收到一个类型加载异常,
System.TypeLoadException was unhandled
Message="Could not load type 'Sample.OutterType' from assembly 'Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field."
如果我将目标 cpu 强制为 32,它工作正常。
此外,如果我将 InnerType 从类更改为结构,它也可以工作。有人可以解释发生了什么或我做错了什么吗?
谢谢