.NET 4.6之前Buffer.MemoryCopy的最佳替代

时间:2019-01-31 04:07:53

标签: c# .net

我正在尝试将某些.NET 4.6代码降级为.NET 4.5。

这是我目前正在使用的代码块:

fixed (byte* destination = dataBytes)
{
    Buffer.MemoryCopy(data, destination, dataLength, dataLength);
}

databyte*类型,所以我不知道Buffer.BlockCopy()是否是一个合理的替代,因为它采用了数组。

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

您是正确的,CALL algo.unionFind.stream('', ':pnHours', {}) YIELD nodeId,setId // groupBy setId, storing all node ids of the same set id into a list MATCH (node) where id(node) = nodeId WITH setId, collect(node) as nodes // order by the size of nodes list descending ORDER BY size(nodes) DESC LIMIT 1 // limiting to 3 RETURN nodes; 是.Net 4.6或更高版本,Buffer.MemoryCopy没有所需的重载,并且Buffer.BlockCopy也不可能。

您可以使用以下内容,但是会很慢

Array.Copy

如果其他所有方法均失败,则可以从msvcrt.dll调用memcpy

fixed (byte* pSource = source, pTarget = target)
    for (int i = 0; i < count; i++)
        pTarget[targetOffset + i] = pSource[sourceOffset + i];

对于.Net 4.5,您也可以使用System.Runtime.CompilerServices.Unsafe

Unsafe.CopyBlock Method

[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);
  

从1复制指定为长整数值的多个字节   内存中的另一个地址。


最后,如果您不介意以数组结尾,可以使用Marshal.Copy。但是它没有指向指针重载的指针。

  

将数据从托管数组复制到非托管内存指针,或者   从非托管内存指针到托管数组。