2

I want to serialize an object in C++, send it to c# and deserialize it there. What is the fastest way to do this for a simple class such as:

class Person
{
    int id;
    string name;
}
Ramesh Verma
  • 111
  • 1
  • 9
  • try this might it will help on your question [check here](https://stackoverflow.com/questions/13275264/sharing-variables-between-c-sharp-and-c) – Bharath_Developer Jul 19 '18 at 08:13

1 Answers1

2

The C++ protogen tools really want you to use a .proto schema, so: you need to describe your intent in .proto; fortunately, that's simple:

syntax = "proto3";
message Person {
    int32 id = 1;
    string name = 2;
}

Then, at least for the C++ part, you need to run that through google's protoc tooling to output the C++ stubs, and work with that. For the C# part, you have (at least) 2 choices: you can do the same thing with protoc to generate C#, or you can use protobuf-net which is happy to work either with types generated from .proto (via protogen - protobuf-net's equivalent of protoc), or from simple annotated types. For example, in C# with protobuf-net your above type will work with simply:

[ProtoContract]
class Person
{
    [ProtoMember(1)]
    int id;
    [ProtoMember(2)]
    string name;
}

In either case (protoc or protogen), you might find this online tool useful in terms of playing with the code-gen step.

That done: use the library-specific serialize mechanism, and you should be fine.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900