1

当尝试使用已通过反射加载的 proto 序列化程序序列化层次结构类型时,它似乎有一些奇怪的行为,并且似乎并没有真正起作用。

这是代码:

    [ProtoContract]
    [ProtoInclude(10, typeof(Derived))]
    class Base
    {
        [ProtoMember(1)]
        public string BaseFirstProperty { get; set; }
        [ProtoMember(2)]
        public string BaseSecProperty { get; set; }
    }

    [ProtoContract]
    class Derived : Base
    {
        [ProtoMember(1)]
        public string DerivedFirstProperty { get; set; }
    }

    static void Main(string[] args)
    {
        var assembly = Assembly.LoadFile(@"c:\protobuf-net.dll");

        var derived = new Derived()
        {
            BaseFirstProperty = "BaseFirst",
            BaseSecProperty = "BaseSec",
            DerivedFirstProperty = "DerivedFirst"
        };

        var reflectionSerializer = assembly.GetType("ProtoBuf.Serializer");
        var getTypeSerializer = typeof(Serializer);

        var reflectionMethods = reflectionSerializer.GetMethods(BindingFlags.Static | BindingFlags.Public);
        var reflectionGenericMethodInfo = reflectionMethods.First<MethodInfo>(method => method.Name == "SerializeWithLengthPrefix");
        var reflectionSpecificMethodInfo = reflectionGenericMethodInfo.MakeGenericMethod(new Type[] { derived.GetType() });

        var getTypeMethods = getTypeSerializer.GetMethods(BindingFlags.Static | BindingFlags.Public);
        var getTypeGenericMethodInfo = getTypeMethods.First<MethodInfo>(method => method.Name == "SerializeWithLengthPrefix");
        var getTypeSpecificMethodInfo = getTypeGenericMethodInfo.MakeGenericMethod(new Type[] { derived.GetType() });

        var reflectionStream = new MemoryStream();
        var getTypeStream = new MemoryStream();
        reflectionSpecificMethodInfo.Invoke(null, new object[] { reflectionStream, derived, PrefixStyle.Base128 });
        getTypeSpecificMethodInfo.Invoke(null, new object[] { getTypeStream, derived, PrefixStyle.Base128 });

        Console.WriteLine(reflectionStream.ToArray().Length); // Prints out 15
        Console.WriteLine(getTypeStream.ToArray().Length); // Prints out 37
    }

据我所知,它应该是一样的......我做错了什么?请注意,我使用的是 Proto-buf 2.0.0.431。谢谢,

4

2 回答 2

0

错误的排序

[ProtoContract]
[ProtoInclude(10, typeof(Derived))]
class Base
{
    [ProtoMember(1)]
    public string BaseFirstProperty { get; set; }
    [ProtoMember(2)]
    public string BaseSecProperty { get; set; }
}

[ProtoContract]
class Derived : Base
{
    [ProtoMember(3)]
    public string DerivedFirstProperty { get; set; }
}
于 2013-01-26T20:01:29.847 回答
0

好的; 我已经通过当前的主干代码运行它;我最初得到一个错误处理ImplicitFields,但我已经在本地更正了。然后运行它,我得到:

37
37

这向我表明,如果最后一次 (625-431)=194 提交,问题已经解决。别问我是哪一个!我建议您尝试 r625 看看效果如何,我还没有部署它,但是应该构建 protobuf-net 项目,或者如果您希望我通过电子邮件将其发送给您,请告诉我。

另外:您可能会发现使用非通用 API 比使用MakeGenericMethod; 或者ProtoBuf.Serializer.NonGeneric,或者ProtoBuf.RuntimeTypeModel.Default(基本上,通用 SerializerAPI 现在只是转发到非通用API,因为 v2 核心删除了所有通用代码;使用通用 API 绝对没有任何优势,除了方便 - 你使用反射时没有)。

于 2013-01-28T22:25:34.043 回答