本文介绍了C#Marshal.Copy IntPtr的16位管理的无符号整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C#Marshal.Copy程序没有任何过载从非托管内存指针复制到16位管理无符号整数数组

Why does C# Marshal.Copy routine does not have any overload for copying from unmanaged memory pointer to 16 bit managed unsigned integer array?

例如:

Copy(IntPtr, Byte[], Int32, Int32)  Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.
Copy(IntPtr, Char[], Int32, Int32)  Copies data from an unmanaged memory pointer to a managed character array.
Copy(IntPtr, Double[], Int32, Int32)    Copies data from an unmanaged memory pointer to a managed double-precision floating-point number array.
Copy(IntPtr, Int16[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 16-bit signed integer array.
Copy(IntPtr, Int32[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array.
Copy(IntPtr, Int64[], Int32, Int32) Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array.
Copy(IntPtr, IntPtr[], Int32, Int32)    Copies data from an unmanaged memory pointer to a managed IntPtr array.
Copy(IntPtr, Single[], Int32, Int32).   Copies data from an unmanaged memory pointer to a managed single-precision floating-point number array.

如果没有编组的选择,我怎么复制非托管USHORT阵列管理USHORT阵列?

If there is no marshalling alternative, how do I copy unmanaged ushort array to managed ushort array?

推荐答案

我觉得 元帅没有一个复制(IntPtr的,UINT16 [],的Int32,Int32)将,因为CLS遵从性(CLS遵从的信条超载是无符号整数运算不暴露给消费者,我不同意这一规则FWIW)。

I think Marshal doesn't have a Copy(IntPtr, UInt16[], Int32, Int32) overload because of CLS compliance (a tenet of CLS compliance is that unsigned integer operations are not exposed to consumers, I disagree with this rule FWIW).

通过编组,所有重要的是该元素的大小。签名岬只是一个次要问题。我要么使用字节的Int16 过载和做自己的铸造之后,或者我会使用不安全的指针。

With marshalling, all that matters is the size of the elements. Signed-ness is just a secondary concern. I would either use the Byte or Int16 overloads and do my own casting afterwards, or I would use unsafe pointers.

IntPtr unmanagedArray = ...

Int16[] destination0 = new Int16[ count ];
Marshal.Copy( unmanagedArray, destination0, 0, count );

UInt16[] destinion1 = destination0.OfType<UInt16>().ToArray(); // Linq extension method

这篇关于C#Marshal.Copy IntPtr的16位管理的无符号整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:37