【问题标题】:Protobuf.NET usingProtobuf.NET 使用
【发布时间】:2011-10-09 22:19:32
【问题描述】:

需要在托管 c# 和非托管 c++ 之间发送一些数据。经过一番研究 我尝试使用 Protobuf.NET。

我不确定我是否了解 ProtoBuf 的功能...

  1. 在 Proto 中构建类型定义。我在 c++ 和 c# 项目中都需要这种类型定义
  2. 使用命令行工具“protogen.exe”从类型定义中获取 .cs 文件和 .cpp、.h
  3. 将 .cs 文件复制到我的 c# 项目中,并将 .cpp、.h 复制到我的 c++ 解决方案中。

似乎我很愚蠢地解决了这个问题。这是我的问题和疑问。

  1. 是否可以在 c# 中定义类型以在 c++ 中生成文件?
  2. 尝试使用命令行工具 protogen.exe 处理以下文件

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;
}

没有什么对我有用。真的什么都试过了。我将原始文件放入 命令行目录,尝试了每个选项来设置目录。如何轻松构建它们? 第二个文件正在使用 c++ 的标准 proto 命令行工具,但我也需要它用于 c#。真的需要你的帮助。

【问题讨论】:

    标签: c# c++ serialization protobuf-net


    【解决方案1】:

    虽然我对 ProtoBuf 了解不多,但我知道有一种更简单的方法可以做到这一点。如果上面的类代表您尝试发送到 C++ 的数据,只需使用 Serialization 并序列化您的类,然后您可以将类和数据输出到 XML 文件。然后您可以使用XmlLite 或其他一些XML 阅读器来读取您的类和数据。你甚至可以使用Boost 来读入序列化的类。

    【讨论】:

    • 嗯,没有。编写解析代码远没有运行 protobuf 编译器那么容易。
    • @Ben -- 谁说过要编写解析代码? .NET 和 Boost 为您完成所有工作。你不需要解析任何东西
    • @Ben 实际上,“仅使用序列化”这一行让我很开心……而 protobuf 是 什么,究竟是什么?
    • @icemanind:Boost 可能会完成繁重的工作(将 XML 转换为文档模型),但您仍然需要编写代码来将数据从标签和属性中提取到 C++ 对象中。这很重要,protobuf 对此有很大帮助。更不用说比 XML 更高效(在空间和计算方面)。
    【解决方案2】:

    test1.proto 不是有效的.proto 文件。

    test2.proto 看起来不错。请注意,您需要来自主 protobuf 项目的编译器才能生成 C++ 代码,需要来自 protobuf-net 的编译器才能生成 C# 代码。我想你的大部分问题来自于没有意识到你需要两个不同的编译器(但是你将相同的.proto 文件提供给两者)。

    【讨论】:

      【解决方案3】:

      首先,请注意 protobuf-net 只是 一个 可用于 .NET 的实现;反正...

      "test1.proto" 不是 .proto - 它是 C#; .proto 对于与 protobuf-net 一起使用不是必需,但在您的互操作场景中,这是一个非常好的主意。有 VS2010 插件,或者 protobuf-net zip 中的 protogen 工具:

       protogen -i:test2.proto -o:test2.cs
      

      这应该会生成 test2.cs 的内容:

      //------------------------------------------------------------------------------
      // <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); }
        }
      
      }
      

      请注意,如果您需要其他开关,例如,尝试大小写规范化(因此您会得到 IdNameEmail 等),或者希望它包含额外的序列化程序支持(@ 987654328@、DataContractSerializer等)

      【讨论】:

      • 我想从一个 proto 文件生成一个类似的文件。我在哪里可以找到 protogen 工具?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2011-06-22
      • 2010-11-25
      • 1970-01-01
      相关资源
      最近更新 更多