C# 有没有办法用描述符proto反序列化未知对象?

C# 有没有办法用描述符proto反序列化未知对象?,c#,protocol-buffers,protobuf-net,C#,Protocol Buffers,Protobuf Net,我想找到一种用描述符proto反序列化未知对象的方法。当我序列化一个人时,这不是问题,因为我知道它的类型。但是当我反序列化时,我不知道我接收到的对象的类型,那么如何使用描述符proto(它定义了proto的结构)来创建像ExpandObject这样的对象呢 这是我的班级: class Person { [ProtoMember(1)] public int Id { get; set; } [ProtoMember(2)]

我想找到一种用描述符proto反序列化未知对象的方法。当我序列化一个人时,这不是问题,因为我知道它的类型。但是当我反序列化时,我不知道我接收到的对象的类型,那么如何使用描述符proto(它定义了proto的结构)来创建像ExpandObject这样的对象呢

这是我的班级:

    class Person
    {
        [ProtoMember(1)]
        public int Id { get; set; }
        [ProtoMember(2)]
        public string Name { get; set; }
    }
我创建了一个带有一些值的对象Person,将其序列化并创建my.proto:

var person = new Person
{
   Id = 12345,
   Name = "Fred"
};
using (var file = File.Create(@"C:\temp\protobuf\person.bin"))
{
   Serializer.Serialize(file, person);
   file.SetLength(file.Position);
}
string proto = Serializer.GetProto<Person>();
File.WriteAllText(@"C:\temp\protobuf\person.proto", proto);
最后,我想用person.pb的描述反序列化我的person.bin,如下所示:

byte[] payload = System.IO.File.ReadAllBytes(@"C:\temp\protobuf\person.pb");
FileDescriptorSet fileDescriptorSet = FileDescriptorSet.ParseFrom(payload);
FileDescriptorProto fileDescriptorProto = fileDescriptorSet.GetFile(0);
DescriptorProto requestDescriptor = fileDescriptorProto.GetMessageType(0);

/**
             requestDescriptor : 

            {name: "Person"
                field {
                  name: "Id"
                  number: 1
                  label: LABEL_OPTIONAL
                  type: TYPE_INT32
                  default_value: "0"
                }
                field {
                  name: "Name"
                  number: 2
                  label: LABEL_OPTIONAL
                  type: TYPE_STRING
                }
             }

             **/

object youhou;
using (var file = File.OpenRead(@"C:\temp\protobuf\person.bin"))
{
    //youhou = Serializer.Deserialize<Person>(file);
    // I don't know **Person** so i can't do this
}
byte[]payload=System.IO.File.ReadAllBytes(@“C:\temp\protobuf\person.pb”);
FileDescriptorSet FileDescriptorSet=FileDescriptorSet.ParseFrom(有效载荷);
FileDescriptorProto FileDescriptorProto=fileDescriptorSet.GetFile(0);
DescriptorProto requestDescriptor=fileDescriptorProto.GetMessageType(0);
/**
请求描述符:
{姓名:“人”
场{
姓名:“Id”
编号:1
标签:标签(可选)
类型:type_INT32
默认值:“0”
}
场{
姓名:“姓名”
编号:2
标签:标签(可选)
类型:类型\字符串
}
}
**/
对象有厚;
使用(var file=file.OpenRead(@“C:\temp\protobuf\person.bin”))
{
//youhou=序列化程序。反序列化(文件);
//我不认识**人**,所以我不能这么做
}

起初,我想创建一个字符串、对象的字典,而不是Person,但不可能序列化它。

我从protobuf网络(标签)的角度回答这个;如果你使用谷歌的C#API,情况会有所不同

这里有一些不同的东西;然而,protobuf net目前没有专门使用描述符加载东西的API!它确实有一个
Extensible
基类,可用于反序列化任何任意负载,以及
Extensible
上的
static
方法,如
Extensible.GetValue(1)
。所以这使您能够读取(或写入)任意负载。您可能需要创建一个非抽象类来使用,但是
class Foo:Extensible{}
应该足够了,然后
Serializer.Deserialize(source)


所以,;第二部分是如何解析描述符。关于这一点,请参阅软件包。这包括您期望的常见描述符类型,包括
FileDescriptorSet
,因此:您可以使用protobuf net的常规
序列化程序。反序列化(源)
以获取描述符集,从中可以用通常的方式分离模型并发现属性,要与可扩展的
一起使用

是的,我今天发现我混合了protobuf net和Google C#API。谢谢你的回答,我将看一看可扩展基类,因为它可能会回答我的问题。
byte[] payload = System.IO.File.ReadAllBytes(@"C:\temp\protobuf\person.pb");
FileDescriptorSet fileDescriptorSet = FileDescriptorSet.ParseFrom(payload);
FileDescriptorProto fileDescriptorProto = fileDescriptorSet.GetFile(0);
DescriptorProto requestDescriptor = fileDescriptorProto.GetMessageType(0);

/**
             requestDescriptor : 

            {name: "Person"
                field {
                  name: "Id"
                  number: 1
                  label: LABEL_OPTIONAL
                  type: TYPE_INT32
                  default_value: "0"
                }
                field {
                  name: "Name"
                  number: 2
                  label: LABEL_OPTIONAL
                  type: TYPE_STRING
                }
             }

             **/

object youhou;
using (var file = File.OpenRead(@"C:\temp\protobuf\person.bin"))
{
    //youhou = Serializer.Deserialize<Person>(file);
    // I don't know **Person** so i can't do this
}