diff options
author | Kevin Fort <kevin.fort@gmail.com> | 2019-09-10 19:07:46 -0500 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-09-10 17:07:46 -0700 |
commit | 2706381eef639dc19eab2b823bc86d2b8521e230 (patch) | |
tree | c20afacccd03925a461f54a9f33b8cd7a4c0ee68 /net/FlatBuffers | |
parent | b5560fcd525a7b45ee45c8037ec5f95a5ea81439 (diff) | |
download | flatbuffers-2706381eef639dc19eab2b823bc86d2b8521e230.tar.gz flatbuffers-2706381eef639dc19eab2b823bc86d2b8521e230.tar.bz2 flatbuffers-2706381eef639dc19eab2b823bc86d2b8521e230.zip |
Add element size parameter to __vector_as_arraysegment [c#] (#5512)
* Add element size parameter to __vector_as_arraysegment
Add element size parameter to __vector_as_arraysegment fixing issue where VectorAsBytes returns incorrect size span for multibyte element types.
* Update codegen
Update codegen and Table to return typed span.
* update test files
update test files
Diffstat (limited to 'net/FlatBuffers')
-rw-r--r-- | net/FlatBuffers/Table.cs | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/net/FlatBuffers/Table.cs b/net/FlatBuffers/Table.cs index e10ba95e..b4863e20 100644 --- a/net/FlatBuffers/Table.cs +++ b/net/FlatBuffers/Table.cs @@ -16,6 +16,7 @@ using System; using System.Text; +using System.Runtime.InteropServices; namespace FlatBuffers { @@ -89,17 +90,23 @@ namespace FlatBuffers // Get the data of a vector whoses offset is stored at "offset" in this object as an // Spant<byte>. If the vector is not present in the ByteBuffer, // then an empty span will be returned. - public Span<byte> __vector_as_span(int offset) + public Span<T> __vector_as_span<T>(int offset, int elementSize) where T : struct { + if (!BitConverter.IsLittleEndian) + { + throw new NotSupportedException("Getting typed span on a Big Endian " + + "system is not support"); + } + var o = this.__offset(offset); if (0 == o) { - return new Span<byte>(); + return new Span<T>(); } var pos = this.__vector(o); var len = this.__vector_len(o); - return bb.ToSpan(pos, len); + return MemoryMarshal.Cast<byte, T>(bb.ToSpan(pos, len * elementSize)); } #else // Get the data of a vector whoses offset is stored at "offset" in this object as an |