问题
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; and get 8 bytes back
style:
public : Byte[8] Foo(Byte[8] data)
EDIT:
can any1 explain why its impossbile in safe context?
回答1:
C# does not allow you to do that. You'll simply have to validate the array's length and maybe throw an exception if the length is not 8.
Also, the type of your function can't be Byte[8]
; you'll have to change that to Byte[]
.
回答2:
If you want to force exactly 8 bytes... consider sending a long
or ulong
instead. 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)
回答3:
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.
As Marc correctly says, fixed size buffers are no fun to work with. You'll probably find it more convenient to do runtime length checking.
来源:https://stackoverflow.com/questions/13798147/fixed-size-byte-array