protobuf 和 .net - 如何使用生成的 .cs 文件?
protobuf and .net - how do I use the generated .cs file?
我开始使用 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 文件是否正确?
.net 中有两个(至少)完全独立的工具可以对话 "protobuf"。看起来您正在使用一个工具中的 工具 (Jon 的版本,它曾经是“protobuf-csharp-port”,但现在是 Google 代码库的一部分IIRC),但使用来自另一个 (protobuf-net) 的库。那行不通的!库必须匹配的工具。
如果你想使用 protobuf-net:要么单独使用 protobuf-net 库(它支持 "code first",意思是:没有工具),要么使用 protobuf-net 工具和 protobuf-net图书馆。
如果您想使用 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>();
}
好的 - 我正在学习更多,这是提供最接近我预期格式的文件生成器: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); }
}
}
我开始使用 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 文件是否正确?
.net 中有两个(至少)完全独立的工具可以对话 "protobuf"。看起来您正在使用一个工具中的 工具 (Jon 的版本,它曾经是“protobuf-csharp-port”,但现在是 Google 代码库的一部分IIRC),但使用来自另一个 (protobuf-net) 的库。那行不通的!库必须匹配的工具。
如果你想使用 protobuf-net:要么单独使用 protobuf-net 库(它支持 "code first",意思是:没有工具),要么使用 protobuf-net 工具和 protobuf-net图书馆。
如果您想使用 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>();
}
好的 - 我正在学习更多,这是提供最接近我预期格式的文件生成器: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); }
}
}