我认为最好的办法将是你的类更改为一个结构和使用Array.Copy,如果可能的话。
但是请注意,用于拷贝到同一个数组中的“Array.Copy”执行速度很慢,速度很慢(比C++慢两倍)。 (如2017年6月1日的)
这是代码,我做调用外部C代码部分:
[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false), SuppressUnmanagedCodeSecurity]
public static unsafe extern void* CopyMemory(void* dest, void* src, ulong count);
// ************************************************************************
/// <summary>
/// Insert an "item" at "index" position into an "array" which could be bigger
/// than the real count of items ("countOfValidItems") in it.
/// </summary>
/// <param name="array"></param>
/// <param name="item"></param>
/// <param name="index"></param>
/// <param name="countOfValidItems"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe public static void InsertItemPinvokeC(Point[] array, Point item, int index, ref int countOfValidItems)
{
if (countOfValidItems >= array.Length)
{
Point[] dest = new Point[array.Length * 2];
// The next 2 calls are replaced by a call to CopyMemory (memcopy from defined in C library "msvcrt.dll").
// Array.Copy(array, 0, dest, 0, index);
// Array.Copy(array, index, dest, index + 1, countOfValidItems - index);
//Buffer.BlockCopy(array, 0, dest, 0, (countOfValidItems - index) * sizeof(Point));
//Buffer.BlockCopy(array, index * sizeof(Point), dest, (index + 1) * sizeof(Point), (countOfValidItems - index) * sizeof(Point));
fixed (Point* s = array)
{
fixed (Point* d = dest)
{
CopyMemory(d, s, (ulong)(index * sizeof(Point)));
CopyMemory(d + ((index + 1) * sizeof(Point)), s + (index * sizeof(Point)), (ulong)((countOfValidItems - index) * sizeof(Point)));
}
}
array = dest;
}
else
{
// Array.Copy(array, index, array, index + 1, countOfValidItems - index);
// Buffer.BlockCopy(array, index * sizeof(Point), array, (index + 1) * sizeof(Point), (countOfValidItems - index) * sizeof(Point));
fixed (Point* p = array)
{
CopyMemory(p + ((index + 1) * sizeof(Point)), p + (index * sizeof(Point)), (ulong)((countOfValidItems - index) * sizeof(Point)));
}
}
array[index] = item;
countOfValidItems++;
}
TransformData不是原始的。如果你试图写出一个TransformData数组,那不是一个原始数组 – Joe