c# - 尝试从 flatbuffer 的二进制文件访问 "LengthofTable"时出现 SystemAccessOutOfbound 异常

标签 c# c++ flatbuffers

我正在根据以下架构存储我的数据。

namespace iCalendarParser;

table EventParsed {

UID:string;
Organizer:string;
Summary:string;
Description:string;
Attendee:[string];
Categories:string;
DtEnd:string;
DtStart:string;
DtStamp:string;
AttendeeCounter:short;

}

table TableofEvents{

  events:[EventParsed];

}

root_type TableofEvents;

我在我的 C++ 应用程序中存储这样的数据。

   std::vector<flatbuffers::Offset<flatbuffers::String>> ListOfAttendee;
   std::vector<flatbuffers::Offset<iCalendarParser::EventParsed>> ListofEvents;



    for (auto EventList : ListofAllEvents)
    {

auto Organizer = builder.CreateString(EventList->Organizer);
auto UID = builder.CreateString(EventList->UID);
auto DtStart = builder.CreateString(EventList->DtStart);
auto DtEnd = builder.CreateString(EventList->DtEnd);
auto DtStamp = builder.CreateString(EventList->DtStamp);
auto Summary = builder.CreateString(EventList->Summary);
auto Description = builder.CreateString(EventList->Description);
auto Categories = builder.CreateString(EventList->Categories);

for (int i = 0; i < EventList->AttendeeCounter; i++)
{
    ListOfAttendee.push_back(builder.CreateString(EventList->Attendee[i]));

}


auto VectorListofAttendee = builder.CreateVector(ListOfAttendee);

auto CreatEventInFlatBuffer = CreateEventParsed(builder, UID, Organizer, Summary, Description, VectorListofAttendee, Categories, DtEnd, DtStart, DtStamp, EventList->AttendeeCounter);

ListofEvents.push_back(CreatEventInFlatBuffer);

 }

 auto CreatVectorOfEventInFlatbuffer = builder.CreateVector(ListofEvents);
 auto InsertEventInFlatBufferList = CreateTableofEvents(builder, 
 CreatVectorOfEventInFlatbuffer);
 builder.Finish(InsertEventInFlatBufferList);

 uint8_t *buf = builder.GetBufferPointer();
 int size = builder.GetSize();

 std::ofstream ofile("icalBin.bin", std::ios::binary);
 ofile.write((char *)&buf, size);
 ofile.close();

我正在尝试从我的 C# 应用程序访问反序列化数据。

byte[] ReadbytesfromFile = File.ReadAllBytes("icalBin.bin");
var buffer = new ByteBuffer(ReadbytesfromFile);
var tableofEvents = TableofEvents.GetRootAsTableofEvents(buffer); 
int len = tableofEvents.EventsLength;

我不知道哪里出了问题。谁能告诉我如何根据架构访问嵌套表并将其反序列化为 C#。

总体思路:

1.获取事件列表。

2.访问单个事件结构。 (我想得到的结果)

最佳答案

我在您的代码中发现了 2 个问题:

  • 它缺少 ListOfAttendee.clear(),您的嵌套循环导致此列表在每次迭代中变大。
  • 您正在将指针数据写入文件,而不是指针指向的内容(删除 &)。

关于c# - 尝试从 flatbuffer 的二进制文件访问 "LengthofTable"时出现 SystemAccessOutOfbound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54446371/

相关文章:

当所选项目不可见时,VirtualMode 中的 C# ListView 会闪烁

C++11 隐式转换

c++ - 我的项目在 vs2008 中编译没有任何问题,但在 2010 中却没有?

javascript - 是否可以使用 FlatBuffers 将序列化数据流式传输到文件中?

c# - 如何正确使用模型绑定(bind)在 Razor Pages 中获取单选按钮值

c# - 让方法在 c# 中采用任何数据类型

c# - 如何使用 Excel.Range.set_Value() 指定单个单元格的格式

c++ - 即使检查了两个索引的边界,动态二维数组也存在奇怪的访问冲突

c++ - 在 FlatBuffer 中使用自定义枚举

protocol-buffers - Protocol Buffer 和 Flatbuffer 有什么区别?