I have the following struct that I'm trying to get to behave like a
union, where Unsigned* and Float* are structs.
[StructLayout(LayoutKind.Explicit)]
unsafe public struct Bits64 {
[FieldOffset(0)] public fixed Unsigned8 c[8];
[FieldOffset(0)] public fixed Unsigned16 s[4];
[FieldOffset(0)] public fixed Unsigned32 u[2];
[FieldOffset(0)] public fixed Float32 f[2];
[FieldOffset(0)] public Float64 d;
}
When I try to compile, I get the error:
Fixed size buffer type must be one of the following: bool, byte,
short, int, long, char, sbyte, ushort, uint, ulong, float or double
So I then tried the following and receive the error beneath it at runtime:
[StructLayout(LayoutKind.Explicit, Size=64)]
public class Bits64 {
[FieldOffset(0)] public Unsigned8[] c = new Unsigned8[8];
[FieldOffset(0)] public Unsigned16[] s = new Unsigned16[4];
[FieldOffset(0)] public Unsigned32[] u = new Unsigned32[2];
[FieldOffset(0)] public Float32[] f = new Float32[2];
[FieldOffset(0)] public Float64 d = new Float64();
}
Could not load type 'DIS.Bits64' from assembly 'TEST, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' because it contains an object
field at offset 0 that is incorrectly aligned or overlapped .
Help?