问题
Need to send some data between managed c# and unmanaged c++. After some research I tried to use Protobuf.NET.
I'm not sure if I understand functionality of ProtoBuf...
- Build a type definition in Proto. I need this type definition in both projects c++ and c#
- Use the command-line tool "protogen.exe" to get an .cs file and .cpp, .h from the type definition
- Copy the .cs files into my c# project and the .cpp, .h in my c++ solution.
Seems I'm to stupid to to solve this. Here are my problem and questions.
- Is it possible to define the type in c# to generate the files in for c++ ?
- Tried to use the command-line tool protogen.exe with the following files
test1.proto
using ProtoBuf; namespace ProtocolBuffers { [ProtoContract] class Person { [ProtoMember(1)] public int Id {get;set;} [ProtoMember(2)] public string Name { get; set; } } }
test2.proto
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
Nothing is working for me. Really tried everything. I put the proto files into the commandline dir, tried every option to set the dir. How to build them easily ? The second file is working with standard proto commandline tool for c++ but i need it for c# too. Really need your help.
回答1:
Firstly, note that protobuf-net is just one available implementation for .NET; anyway...
"test1.proto" is not a .proto - it is C#; a .proto is not required for use with protobuf-net, but in your interop scenario it is a very good idea. There is VS2010 plugin for this, or alternatively the protogen
tool that is in the protobuf-net zip:
protogen -i:test2.proto -o:test2.cs
This should generate test2.cs
with the contents:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test2.proto
namespace test2
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Person")]
public partial class Person : global::ProtoBuf.IExtensible
{
public Person() {}
private int _id;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
public int id
{
get { return _id; }
set { _id = value; }
}
private string _name;
[global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"name", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string name
{
get { return _name; }
set { _name = value; }
}
private string _email = "";
[global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"email", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue("")]
public string email
{
get { return _email; }
set { _email = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
Note that there are additional switches if you want, for example, it to attempt case normalization (so you get Id
, Name
, Email
, etc), or want it to include additional serializer support (BinaryFormatter
, DataContractSerializer
, etc)
回答2:
test1.proto
isn't a valid .proto
file.
test2.proto
looks pretty good. Note that you'll need the compiler from the main protobuf project in order to generate the C++ code, and the compiler from protobuf-net in order to generate the C# code. I guess most of your problems come from not realizing you need two different compilers (but you feed the same .proto
file to both).
回答3:
Although I don't know much about ProtoBuf, I do know there is a simpler way of doing this. If the classes above represent the data you are trying to send to C++, just use Serialization and serialize your class, then you can output the classes and data to an XML file. Then you can use XmlLite or some other XML reader to read in your classes and data. You can even use Boost to read in the serialized classes.
来源:https://stackoverflow.com/questions/6771444/protobuf-net-using