C# 如何使用protobuf网络扩展?
我已经创建了一个.proto文件,ProtoBufTool成功地创建了.cs文件。我对csharp有点陌生,我正在尝试设置扩展字段。但不知道怎么做?有人举过如何使用protobuf网络扩展的例子吗 My.proto文件:C# 如何使用protobuf网络扩展?,c#,protobuf-net,C#,Protobuf Net,我已经创建了一个.proto文件,ProtoBufTool成功地创建了.cs文件。我对csharp有点陌生,我正在尝试设置扩展字段。但不知道怎么做?有人举过如何使用protobuf网络扩展的例子吗 My.proto文件: package messages; message DMsg { optional int32 msgtype = 1; extensions 100 to max; } extend DMsg { optional string fltColumns = 1
package messages;
message DMsg
{
optional int32 msgtype = 1;
extensions 100 to max;
}
extend DMsg
{
optional string fltColumns = 101;
}
以下是已创建的类:
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
// Generated from: message.proto
namespace messages
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"DMsg")]
public partial class DMsg : global::ProtoBuf.IExtensible
{
public DMsg() {}
private int _msgtype = default(int);
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"msgtype", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)][global::System.ComponentModel.DefaultValue(default(int))]
public int msgtype
{
get { return _msgtype; }
set { _msgtype = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
protobuf net对扩展没有出色的支持;您需要使用字段编号(我认为它目前对fltColumns
没有任何作用)。但是,要获取值,您应该能够使用Extensible.GetValue
/TryGetValue
(请注意:在C#3.0中使用这些扩展方法)。要设置一个值,请使用AppendValue
——它不知道这是一个值还是一个列表(repeated
),因此相同的API处理这两种情况
可能(更接近Java版本)在这里有更好的支持
示例(为了简洁起见,我使用手工编写的类,但它也应该适用于生成的类型):
static void Main()
{
MyData数据=新的MyData();
数据Id=123;
//我们只知道字段id。。。
可扩展的.AppendValue(数据,27,“我的名字”);
字符串myName=Extensible.GetValue(数据,27);
//这也应该可以(即,如果我们将其加载到
//*是否*理解27表示姓名)
MyKnownData known=序列化程序.ChangeType(数据);
Console.WriteLine(已知的.Id);
Console.WriteLine(已知的.Name);
}
[原始合同]
类MyData:可扩展
{
[原成员(1)]
公共int Id{get;set;}
}
[原始合同]
Myknownda类
{
[原成员(1)]
公共int Id{get;set;}
[原成员(27)]
公共字符串名称{get;set;}
}
您是否有一个示例演示如何设置值和获取值?@VNarasimhaM-根据请求添加到Jon版本的链接已断开。这是我发现的最接近的一个:@JesseChisholm Jon的东西现在被迁移到主要的google impl(尽管有很大的变化):
static void Main()
{
MyData data = new MyData();
data.Id = 123;
// something we know only by field id...
Extensible.AppendValue<string>(data, 27, "my name");
string myName = Extensible.GetValue<string>(data, 27);
// this should be OK too (i.e. if we loaded it into something that
// *did* understand that 27 means Name)
MyKnownData known = Serializer.ChangeType<MyData, MyKnownData>(data);
Console.WriteLine(known.Id);
Console.WriteLine(known.Name);
}
[ProtoContract]
class MyData : Extensible
{
[ProtoMember(1)]
public int Id { get; set; }
}
[ProtoContract]
class MyKnownData
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(27)]
public string Name{ get; set; }
}