本文介绍了ProtoBuf-Net:从父类继承object []类型的[ProtoMember]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

object [] mList持有子集合想要的任何对象.对于其余指定的更多对象集合,这应该是伪包装类.

The object[] mList holds whatever objects that the child collection wants it to.This is supposed to be a dummy wrapper class for the rest of the more specified collections of objects.

[ProtoInclude(98, typeof(Object1CollectionProto))]
[ProtoInclude(99, typeof(Object2CollectionProto))]
[ProtoInclude(100, typeof(Object3CollectionProto))]
public class ObjectCollectionProto
{
  protected ObjectType mCollectionType;
  protected object[] mList;
  public ObjectCollectionProto(){}

  [ProtoMember(1), DefaultValue(ObjectType.Base)]
  public ObjectType CollectionType //enumeration for type
  {
     get { return mCollectionType; }
     set { mCollectionType = value;}
  }

  [ProtoMember(2)]
  public object[] List
  {
     get { return mList; }
     set { mList = value;}
  }
}

然后我们有一个上面的虚拟包装器的子类示例,该子类应该继承其所需类型的object [].

Then we have an example child class of the above dummy wrapper that is supposed to inherit the object[] of it's desired type.

 [ProtoContract]
public class Object1CollectionProto : ObjectCollectionProto
{
  public Object1CollectionProto()
  {
  }
}

我如何指定类层次结构,以便Object1CollectionProto继承一个object [] mList作为可序列化的Object1的列表?就我而言,Object1已经可以序列化了.只是不是它们的集合版本.

How do I go about specifying the class hierarchy so that Object1CollectionProto inherits a the object[] mList as a list of Object1's that can be serialized? Object1's can be serialized in my case already. Just not the collection version of them.

推荐答案

如何序列化数组?这是解决问题的好方法,只需重新构建框架以适合这种序列化数组的方式即可.

How to serialize arrays?This is a good way to solve the problem, just remake the framework to fit this way of serializing arrays.

我还经过一些仔细的思考,提出了一种正确地序列化 array 的方法.就我而言,我从不实际上序列化了 PARENT 类,但是我只序列化了儿童.

Also with some careful thinking I made a way of serializing that array correctly. In my case I NEVER actually serialize the PARENT class, but rather I only serialize the CHILDREN.

我从父类数组中删除了ProtoMember属性,并在子类中设置了该数组,并在CHILDREN类中添加了特定类型.

I removed the ProtoMember attribute from the parent class array and set the array in the children, with a specific type into the CHILDREN classes.

public class BaseCollection{
  protected object[] mList;
  /*
   * this has a lot more properties/functions that declare how I prepare
   * collections for serialization but are superfluous for the conversation,
   * hence the reason for me wanting to know how to do this
  */
}

public class ChildCollection : BaseCollection{
  [ProtoMember(1)]
  public Child[] childCollection
  {
    get { return mList as Child[]; }
    set { mList = value; }
  }
}

这篇关于ProtoBuf-Net:从父类继承object []类型的[ProtoMember]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!