protobuf和.net - 如何使用生成的.cs文件?

时间:2016-04-12 03:47:02

标签: c# protocol-buffers protobuf-net

我开始使用protobufs - 我有一个原型文件:

syntax = "proto2";

message InputState {
    required uint32 input = 1;
    required string state = 2;
}

message InputStateData {
    repeated InputState input = 1;
}

我运行了命令行工具(来自protoc-3.0.0-alpha-3-win32)

我得到了这个意想不到的.cs文件: !click me! Input.cs

我收录了nuget protobuf-portable-net,但现在我不知道如何使用生成的.cs文件

接下来我该怎么做 - Input.cs文件是否正确?

2 个答案:

答案 0 :(得分:1)

.net中有两个完全独立的工具(至少)说“protobuf”。看起来你正在使用工具(Jon的版本,曾经是“protobuf-csharp-port”,但它现在是Google代码库IIRC的一部分),但是使用了库来自另一个(protobuf-net)。那不行!库必须匹配的工具。

如果你想使用protobuf-net:要么自己使用protobuf-net库(它支持“代码优先”,意思是:没有工具),要么使用protobuf-net工具和protobuf-net库。< / p>

如果你想使用protobuf-csharp-port(现在可能只是“protobuf”):使用那个工具,以及那里的工具。

有关信息,对于使用protobuf-net的代码优先方法,如果您不想使用构建步骤,则可以手动翻译简单合同:

[ProtoContract]
class InputState {
    [ProtoMember(1)]
    public uint Input {get;set;}
    [ProtoMember(2)]
    public string State {get;set;}
}
[ProtoContract]
class InputStateData {
    [ProtoMember(1)]
    public List<InputState> Input {get;} = new List<InputState>();
}

答案 1 :(得分:0)

好的 - 我了解更多,这是文件生成器,它提供的格式最接近我的预期:https://github.com/floatinghotpot/protogen(用于protobuf-net)。这正是我所需要的。

//------------------------------------------------------------------------------
// <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: inputs.proto
namespace inputs
{
[global::System.Serializable,  global::ProtoBuf.ProtoContract(Name=@"InputState")]
 public partial class InputState : global::ProtoBuf.IExtensible
{
public InputState() {}

private uint _input;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"input", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
public uint input
{
  get { return _input; }
  set { _input = value; }
}
private string _state;
[global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"state", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string state
{
  get { return _state; }
  set { _state = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
  { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}

 [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"InputStateData")]
 public partial class InputStateData : global::ProtoBuf.IExtensible
{
public InputStateData() {}

private readonly global::System.Collections.Generic.List<InputState> _input = new global::System.Collections.Generic.List<InputState>();
[global::ProtoBuf.ProtoMember(1, Name=@"input", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<InputState> input
{
  get { return _input; }
}

private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
  { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}

}