【发布时间】:2014-07-23 07:40:46
【问题描述】:
我正在尝试从 C union 构建 C# 显式 struct。显式结构是:
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct struct_1
{
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public uint[] All32;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public struct_2[] bits;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct struct_2
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] Var56;
}
这些是结构,在从byte 数组实例化后,我无法访问All32 uint(智能感知显示'?')如下
Type structureType = typeof(struct_1);
byte[] b = new byte[4];
b[0] = 0xA0;
b[1] = 0x01;
b[2] = 0xF0;
b[3] = 0x00;
if (structureType != null)
{
try
{
GCHandle handle = GCHandle.Alloc(b, GCHandleType.Pinned);
struct_1 intpdObj = (struct_1)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), structureType);
handle.Free();
}
catch
{
}
}
【问题讨论】:
-
您的空
catch块可能会抑制有价值的调试信息。 -
我没有任何“?”问题。 However, here is what i got您的第一个字段指向与您的第二个字段相同的地址,并找到 struct_2 而不是 uint(参见我图片的“类型”列)
-
你能发布 C 结构声明吗?