【发布时间】:2021-08-12 07:17:43
【问题描述】:
我是 C# 初学者,有一个问题。
在C编程中,我知道我可以参考以下代码来读取/写入结构成员中的数组数据,而结构有很多成员,如A[128],B[128],...,M[64], N[128],...,X[32],Y[32],Z[128],...,主代码无需添加分配缓冲区代码即可访问缓冲区数组,简单明了。
struct myStructB{
int cnt;
float AA[32];
float BB[32];
float CC[128];
};
struct myStructA{
int cnt;
int A[128];
int B[128];
int C[32];
...
int M[64];
int N[128];
...
int Z[128];
myStructB strctB;
};
myStructA testStruct;
int _tmain(int argc, _TCHAR* argv[])
{
testStruct.A[0] = 123;
testStruct.B[0] = 456;
testStruct.C[0] = 789;
testStruct.strctB.AA[0] = 321;
testStruct.strctB.BB[0] = 654;
testStruct.strctB.CC[0] = 987;
int sss = testStruct.strctB.AA[0];
return 0;
}
在C#中,我想我应该为结构内的数组分配缓冲区,如下所示,如果结构有很多数组元素,代码并不简单,必须分配所有数组成员。我认为这不是一个好主意.....有更好的解决方案来改进此代码吗?我想将此结构对象传递给 C++ DLL。谢谢:)
[StructLayout(LayoutKind.Sequential)]
struct myStructB{
public int cnt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public int[] AA;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public int[] BB;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public int[] CC;
}
[StructLayout(LayoutKind.Sequential)]
struct myStructA{
public int cnt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public int[] A;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public int[] B;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public int[] C;
...
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public int[] M;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public int[] N;
...
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public int [] Z;
...
public myStructB strctB;
}
myStructA testStruct = new myStructA();
testStruct.A = new int[128];
testStruct.B = new int[128];
testStruct.C = new int[32];
...
testStruct.M = new int[64];
testStruct.N = new int[128];
...
testStruct.Z = new int[128];
testStruct.strctB.AA = new int[128];
testStruct.strctB.BB = new int[128];
testStruct.strctB.CC = new int[32];
testStruct.A[0] = 123;
testStruct.B[0] = 456;
testStruct.C[0] = 789;
testStruct.strctB.AA[0] = 321;
testStruct.strctB.BB[0] = 654;
testStruct.strctB.CC[0] = 987;
【问题讨论】: