[英]Fixed Size Byte Array
public : array<Byte>^ Foo(array<Byte>^ data)
gets dynamic size managed array 获取动态大小托管数组
but how can I get fixed size managed byte array? 但是如何获得固定大小的托管字节数组呢?
I wanna force C# user to send me 8 byte array; 我想强迫C#用户发送8字节数组; and get 8 bytes back
并获得8个字节
style: 样式:
public : Byte[8] Foo(Byte[8] data)
EDIT: 编辑:
can any1 explain why its impossbile in safe context? 可以解释为什么它在安全环境中的不可能性?
If you want to force exactly 8 bytes... consider sending a long
or ulong
instead. 如果你想强制使用 8个字节...请考虑发送一个
long
或ulong
。 Old-school, but it works. 老派,但它的确有效。 It also has the advantage of not needing an object (a
byte[]
is an object) - it is a pure value-type (a primitive, in this case) 它还具有不需要对象(
byte[]
是对象)的优点 - 它是纯值类型(在这种情况下是基元)
C# does not allow you to do that. C#不允许你这样做。 You'll simply have to validate the array's length and maybe throw an exception if the length is not 8.
您只需验证数组的长度,如果长度不是8,则可能抛出异常。
Also, the type of your function can't be Byte[8]
; 另外,你的函数类型不能是
Byte[8]
; you'll have to change that to Byte[]
. 你必须把它改成
Byte[]
。
You can use a fixed size buffer inside a struct. 您可以在结构中使用固定大小的缓冲区 。 You'll need it to be in an unsafe block though.
但是你需要它在一个不安全的区域。
unsafe struct fixedLengthByteArrayWrapper
{
public fixed byte byteArray[8];
}
On the C++ side you'll need to use inline_array
to represent this type. 在C ++方面,您需要使用
inline_array
来表示此类型。
As Marc correctly says, fixed size buffers are no fun to work with. 正如Marc所说,固定大小的缓冲区并不适合使用。 You'll probably find it more convenient to do runtime length checking.
您可能会发现进行运行时长度检查更方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.