I'm using an external C library that comes with a C# wrapper file containing a struct like (among many other things):
[StructLayout(LayoutKind.Sequential, Pack = 8)]
unsafe public struct Data
{
public fixed double Values[3];
};
I can get the values by adding an additional method to that struct:
public double[] CopyFixedDoubleArray()
{
double[] result = new double[3];
fixed (double* pDst = result)
{
fixed (double* pSrc = Values)
{
double* pd = pDst;
double* ps = pSrc;
for (int i = 0; i < result.Length; i++)
{
*pd = *ps;
pd++; ps++;
}
}
}
return result;
}
But this is not what I want because I don't want to touch the wrapper file (to avoid having to keep the external file in our SCM repository). Whatever I try to do to get the values from outside that struct results in the following error:
Fixed size buffers can only be accessed through locals or fields
What I've tried:
double a = data.Values[2];double a; unsafe { a = data.Values[2]; }double a; fixed (double* p = data.Values) { a = p[2]; }fixed (double a = data.Values[2]) { /* use a */ }
Is there any way to get around this?
CopyFixedDoubleArray()ifstruct Datacontained theCopyFixedDoubleArray()method.double a = data.CopyFixedDoubleArray()[2];for exampleData data2 = data; double a; unsafe { a = data2.Values[2]; }does not work?