具有继承的复杂类型的protobuf-net数组

时间:2011-08-15 12:20:47

标签: protobuf-net

我有一个层次结构,比如说BaseClass< - DerivedClass 我想序列化一个带有混合DerivedClass和BaseClass对象的BaseClass []数组 我使用V2 Sourcecode Revision r438(2011年8月9日)

伪代码: myArray = new BaseClass [] {new BaseClass(),new DerivedClass()};

我尝试了几个声明,但是我没有得到预期的结果:

[ProtoMember(1, AsReference = true, DynamicType = true)]
private BaseClass[] myArray;

- >不反序列化所有引用都没有设置

[ProtoMember(1, AsReference = true, DynamicType = true)]
private object[] myArray;

- > deserailze,但数组类型从BaseClass []更改为object []

任何建议如何让我原来的BaseClass []数组回来?

[编辑]单元测试:

namespace TechnologyEvaluation.Protobuf.ArrayOfBaseClassTest
{
    using System;
    using NUnit.Framework;
    using ProtoBuf;
    using ProtoBuf.Meta;

    [ProtoContract]
    class BaseClassArrayContainerClass
    {
        [ProtoMember(1, DynamicType = true)]
        public Base[] BaseArray { get; set; }
    }

    [ProtoContract]
    class ObjectArrayContainerClass
    {
        [ProtoMember(1, DynamicType = true)]
        public object[] ObjectArray { get; set; }

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

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

    [TestFixture]
    class ArrayOfBaseClassTests : AssertionHelper
    {
        [Test]
        public void TestObjectArrayContainerClass()
        {
            var model = CreateModel();
            var container = new ObjectArrayContainerClass();
            container.ObjectArray = this.CreateArray();
            var cloned = (ObjectArrayContainerClass) model.DeepClone(container);
            Expect(cloned.ObjectArray, Is.Not.Null);

            foreach (var obj in cloned.ObjectArray )
            {
                Expect(obj as Base, Is.Not.Null);
            }

            Expect(cloned.ObjectArray[1] as Derived, Is.Not.Null);
            Expect(cloned.ObjectArray.GetType(), Is.EqualTo(typeof(Base[])));

        }

        [Test]
        public void TestBaseClassArrayContainerClass()
        {
            var model = CreateModel();
            var container = new BaseClassArrayContainerClass();
            container.BaseArray = this.CreateArray();
            var cloned = (BaseClassArrayContainerClass) model.DeepClone(container);
            Expect(cloned.BaseArray, Is.Not.Null);

            foreach (var obj in cloned.BaseArray)
            {
                Expect(obj as Base, Is.Not.Null);
            }
            Expect(cloned.BaseArray[1] as Derived, Is.Not.Null);
            Expect(cloned.BaseArray.GetType(), Is.EqualTo(typeof(Base[])));
        }

        RuntimeTypeModel CreateModel()
        {
            RuntimeTypeModel model = RuntimeTypeModel.Create();

            model.Add(typeof(ObjectArrayContainerClass), true);
            model.Add(typeof(BaseClassArrayContainerClass), true);
            model.Add(typeof(Base), true);
            model[typeof(Base)].AddSubType(100, typeof(Derived));

            return model;
        }

        Base[] CreateArray()
        {
            return new Base[] { new Base() { BaseClassText = "BaseClassText" }, new Derived() { BaseClassText = "BaseClassText", DerivedClassText = "DerivedClassText" } };
        }
    }
}

0 个答案:

没有答案