mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-27 00:52:17 +00:00
Add Get Bytes Method Generator for C#
I updated idl_gen_general.cpp to add support for generating a Get Bytes method for a vector to the generated C# source code. Given a byte vector field named Foo, a method named GetFooBytes() will be generated in the C# source code that will return an ArraySegment<byte> value referencing the vector data in the underlying ByteBuffer. I added a method to Table.cs named __vector_as_arraysegment that is used by the code generated by the change to the C# generator. __vector_as_arraysegment will take the offset of the vector and will return the ArraySegment<byte> value corresponding to the bytes that store the vector data. I updated FlatBuffersExampleTests.cs to add tests to validate my implementation of Table.__vector_as_arraysegment. I added tests to demonstrate that the bytes for the monster's name can be extracted from the underlying byte array. I also added tests to show that Table.__vector_as_arraysegment returns a null value if the vector is not present in the FlatBuffer. I used the updated flatc.exe program to regenerate the C# source files for the MyGame example. The new Monster class includes the GetXXXBytes methods to return the byte arrays containing data for vectors.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using MyGame.Example;
|
||||
|
||||
namespace FlatBuffers.Test
|
||||
@@ -184,6 +185,18 @@ namespace FlatBuffers.Test
|
||||
Assert.AreEqual("test2", monster.GetTestarrayofstring(1));
|
||||
|
||||
Assert.AreEqual(false, monster.Testbool);
|
||||
|
||||
var nameBytes = monster.GetNameBytes().Value;
|
||||
Assert.AreEqual("MyMonster", Encoding.UTF8.GetString(nameBytes.Array, nameBytes.Offset, nameBytes.Count));
|
||||
|
||||
if (0 == monster.TestarrayofboolsLength)
|
||||
{
|
||||
Assert.IsFalse(monster.GetTestarrayofboolsBytes().HasValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.IsTrue(monster.GetTestarrayofboolsBytes().HasValue);
|
||||
}
|
||||
}
|
||||
|
||||
[FlatBuffersTestMethod]
|
||||
|
||||
Reference in New Issue
Block a user