My C# application interacts with the C application via the "callbacks" mechanism.
On the C side I have Point
struct:
struct Point {
float x, y, z;
};
To transfer points from C to C# I pass my C# delegate callback function pointsCallback
to C function getPoints
.
typedef void(*PointsCallback)(const Point* points, int size);
void getPoints(PointsCallback callback);
delegate void PointsCallback(IntPtr points, int size);
[DllImport(...)]
static extern void getPoints(PointsCallback callback);
My PointsCallback
C# implementation looks like this:
[StructLayout(LayoutKind.Sequential)]
struct Point {
public float x, y, z;
}
// called from c code
void PointsCallbackImpl(IntPtr points, int size) {
var pointSize = Marshal.SizeOf<Point>();
var myPoints = new List<Point>();
for (int i = 0; i < size; ++i) {
myPoints.Add(Marshal.PtrToStructure<Point>(points + i * pointSize));
}
// do something with myPoints
}
The problem is this code is quite slow compared to python's np.array
which allows me to save all points
as a binary array and interpret them via np.dtype
.
Is there any C# analog to np.array
behavior, which allows to store everything in binary form and only interpret data as some structs?