C#如何封装C++嵌套结构体,是结构体里面嵌套了多个结构体

2025-04-13 08:00:06
推荐回答(1个)
回答1:

看你的定义里有数组,有Struct嵌套,给你的例子参考:C++: typedef struct{
BYTE ByteV[10];
} StructA;

typedef struct
{
BYTE ByteV[10];
StructA StructAs[20];

} StructB;

C#: [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public struct StructA
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=10)]
public byte[] ByteV;
};

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct StructB
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=10)]
public byte[] ByteV;

[ MarshalAs( UnmanagedType.ByValArray, SizeConst=20)]
public StructA[] StructAs;
};

-手写的,仅供参考