Protobuf.NET 使用 [英] Protobuf.NET using
问题描述
需要在托管 C# 和非托管 C++ 之间发送一些数据.经过一番研究我尝试使用 Protobuf.NET.
我不确定我是否了解 ProtoBuf 的功能...
- 在 Proto 中构建类型定义.我在 c++ 和 c# 项目中都需要这个类型定义
- 使用命令行工具protogen.exe"从类型定义中获取 .cs 文件和 .cpp、.h
- 将 .cs 文件复制到我的 c# 项目中,并将 .cpp、.h 复制到我的 c++ 解决方案中.
解决这个问题似乎很愚蠢.这是我的问题和问题.
- 是否可以在 c# 中定义类型以生成 c++ 中的文件?
- 尝试将命令行工具 protogen.exe 与以下文件一起使用
<小时>
test1.proto
<块引用>使用ProtoBuf;命名空间协议缓冲区{[原合同]班级人物{[ProtoMember(1)]公共整数 ID {get;set;}[ProtoMember(2)]公共字符串名称{获取;放;}}}
<小时>
test2.proto
message Person {所需的 int32 id = 1;所需的字符串名称 = 2;可选字符串 email = 3;}
<小时>
没有什么对我有用.真的什么都试过了.我把 proto 文件放入命令行目录,尝试了每个选项来设置目录.如何轻松构建它们?第二个文件正在使用用于 c++ 的标准 proto 命令行工具,但我也需要它用于 c#.真的需要你的帮助.
首先,请注意 protobuf-net 只是 .NET 的一个可用实现;无论如何...
"test1.proto" 不是 .proto - 它是 C#;与 protobuf-net 一起使用 不需要 .proto,但在您的互操作场景中,这是一个非常好的主意.有 VS2010 插件,或者 protobuf-net zip 中的 protogen
工具:
protogen -i:test2.proto -o:test2.cs
这应该生成带有内容的 test2.cs
:
//----------------------------------------------------------------//<自动生成>//此代码是由工具生成的.////对此文件的更改可能会导致不正确的行为,如果//重新生成代码.//</自动生成>//------------------------------------------------------------------------------//生成自:test2.proto命名空间 test2{[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Person")]公共部分类 Person : global::ProtoBuf.IExtensible{公共人(){}私有整数_id;[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]公共整数 ID{得到{返回_id;}设置 { _id = 值;}}私人字符串_name;[global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"name", DataFormat = global::ProtoBuf.DataFormat.Default)]公共字符串名称{得到 { 返回 _name;}设置 { _name = 值;}}私人字符串 _email = "";[global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"email", DataFormat = global::ProtoBuf.DataFormat.Default)][全局::System.ComponentModel.DefaultValue("")]公共字符串电子邮件{得到 { 返回 _email;}设置 { _email = 值;}}私有全局::ProtoBuf.IExtension extensionObject;global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing){ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing);}}}
请注意,如果您愿意,还有其他开关,例如,尝试区分大小写(因此您将获得 Id
、Name
、Email
> 等),或者希望它包含额外的序列化器支持(BinaryFormatter
、DataContractSerializer
等)
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.
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)
这篇关于Protobuf.NET 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!