Protobuf-net通用继承和封闭构造泛型类型

时间:2011-06-28 15:43:31

标签: protobuf-net

我有一个相当复杂的继承层次结构,包括泛型,我们正在尝试使用protobuf .net进行序列化。不幸的是,它似乎无法正确处理这种情况。这就是层次结构的样子。

    [System.Runtime.Serialization.DataContract]
    [ProtoBuf.ProtoInclude(1000, typeof(GenericBaseClass<object>))]
    [ProtoBuf.ProtoInclude(1001, typeof(GenericBaseClass<string>))]
    public abstract class BaseClass
    {

        public int BaseProperty1 { set; get; }
        public int BaseProperty2 { set; get; }

        public BaseClass()
        {

        }

    }

    [System.Runtime.Serialization.DataContract]
    [ProtoBuf.ProtoInclude(1002, typeof(GenericDerivedClass<object>))]
    [ProtoBuf.ProtoInclude(1003, typeof(GenericDerivedClass<string>))]
    public abstract class GenericBaseClass<T> : BaseClass
    {
        /// <summary>
        /// 
        /// </summary>
        [System.Runtime.Serialization.DataMember(Order = 5)]
        public T ResponseProperty
        {
            get;
            set;
        }

        public GenericBaseClass()
        {
        }
    }

    [System.Runtime.Serialization.DataContract]
    [ProtoBuf.ProtoInclude(1004, typeof(DerivedClass1))]
    [ProtoBuf.ProtoInclude(1005, typeof(DerivedClass2))]
    public abstract class GenericDerivedClass<T> : GenericBaseClass<T>
    {
        public int AdditionalProperty { get; set; }

        public GenericDerivedClass()
        {

        }
    }

最后,这些类由两个封闭构造的非泛型类

实现
    [System.Runtime.Serialization.DataContract]
    public class DerivedClass1 : GenericDerivedClass<string>             
    {
        [System.Runtime.Serialization.DataMember(Order = 6)]
        public int DerivedClass1Property { set; get; }
    }

    [System.Runtime.Serialization.DataContract]
    public class DerivedClass2 : GenericDerivedClass<object>
    {
        [System.Runtime.Serialization.DataMember(Order = 7)]
        public int DerivedClass2Property { set; get; }
    }

我已经编写了以下测试方法来序列化这些并且它给了我错误。

    [TestMethod]
    public void SerializeDeserializeAndCompare()
    {            

        DerivedClass2 i = new DerivedClass2() { BaseProperty1 = 1, BaseProperty2 = 2, DerivedClass2Property = 3, ResponseProperty = new Object() };
        using (var file = System.IO.File.Create("test.bin"))
        {
            ProtoBuf.Serializer.Serialize(file, i);
        }

        using (var file = System.IO.File.OpenRead("test.bin"))
        {
            var o = ProtoBuf.Serializer.Deserialize<DerivedClass2>(file);
        }
    }

我得到的错误是

ProtoBuf.ProtoException:一个类型只能参与一个继承层次结构(CapitalIQ.DataGet.UnitTests.DataSetUnitTest + DerivedClass2)---&gt; System.InvalidOperationException:类型只能参与一个继承层次结构

这是protobuf .net的限制还是我做错了。我使用的是r282版本。

由于 Shobhit

1 个答案:

答案 0 :(得分:4)

与所有属性一样,属性中包含的类型信息适用于泛型类型定义中的所有封闭类型。因此,您实际定义的(对protobuf-net)是:

BaseClass
: GenericBaseClass<object>
 : GenericDerivedClass<object>
  : DerivedClass1
  : DerivedClass2
 : GenericDerivedClass<string>
  : DerivedClass1
  : DerivedClass2
: GenericBaseClass<string>
 : GenericDerivedClass<object>
  : DerivedClass1
  : DerivedClass2
 : GenericDerivedClass<string>
  : DerivedClass1
  : DerivedClass2

正如你所看到的,有很多重复 - 这显然令人困惑。由于属性参数不能使用类型参数,因此可以选择添加某种奇怪的谓词机制,这对IMO来说非常困惑。 IMO,手动建模(删除ProtoInclude属性)会更好。我怀疑您想要的型号是:

BaseClass
: GenericBaseClass<object>
 : GenericDerivedClass<object>
  : DerivedClass2
: GenericBaseClass<string>
 : GenericDerivedClass<string>
  : DerivedClass1

protobuf-net可以使用它,但是解释模型需要&#34; v2&#34;和RuntimeTypeModel

另请注意,object对于protobuf来说有点问题; protobuf-net可以使用动态类型选项伪造它,但那是......不理想。它当然不能序列化object,所以对于测试我已经替换了一个字符串。另请注意,BaseProperty1BaseProperty2AdditionalProperty目前尚未标记为序列化,但可以简单。

反正:

RuntimeTypeModel.Default[typeof(BaseClass)]
    .AddSubType(10, typeof(GenericBaseClass<object>))
    .AddSubType(11, typeof(GenericBaseClass<string>));

RuntimeTypeModel.Default[typeof(GenericBaseClass<object>)]
    .AddSubType(10, typeof(GenericDerivedClass<object>));
RuntimeTypeModel.Default[typeof(GenericBaseClass<object>)][5].DynamicType = true; // object!
RuntimeTypeModel.Default[typeof(GenericDerivedClass<object>)]
    .AddSubType(10, typeof(DerivedClass2));

RuntimeTypeModel.Default[typeof(GenericBaseClass<string>)]
    .AddSubType(10, typeof(GenericDerivedClass<string>));
RuntimeTypeModel.Default[typeof(GenericDerivedClass<string>)]
    .AddSubType(10, typeof(DerivedClass1));

DerivedClass2 i = new DerivedClass2() { BaseProperty1 = 1, BaseProperty2 = 2, DerivedClass2Property = 3, ResponseProperty = "some string" };
using (var file = System.IO.File.Create("test.bin"))
{
    ProtoBuf.Serializer.Serialize(file, i);
}

using (var file = System.IO.File.OpenRead("test.bin"))
{
    var o = ProtoBuf.Serializer.Deserialize<DerivedClass2>(file);
}

你没有 使用RuntimeTypeModel.Default - 事实上,我建议使用(和缓存)一个单独的类型模型;但是Serializer.Serialize指向默认模型。如果您创建自定义模型(TypeModel.Create),只需将其存储在某处并使用Serialize等。