这个问题是从Marshalling C# structure 到 C++ Using StructureToPtr的后续问题。我有以下结构:
[StructLayout(LayoutKind.Explicit, Size = 120, CharSet = CharSet.Unicode)]
public unsafe struct DynamicState
{
[FieldOffset(0)]
public fixed double Position[3];
[FieldOffset(24)]
public fixed double Velocity[3];
[FieldOffset(48)]
public fixed double Acceleration[3];
[FieldOffset(72)]
public fixed double Attitude[3];
[FieldOffset(96)]
public fixed double AngularVelocity[3];
}
如果我尝试像这样初始化一个数组:
var dynamicState = new DynamicState();
double[] array = new double[] { 1, 2, 3 };
fixed (double* pArray = array)
{
dynamicState.Acceleration = pArray;
}
我收到以下错误:The left-hand side of an assignment must be a variable, property or indexer
。
初始化作为结构一部分的不安全数组的正确方法是什么?