ProtoBuf-Net:从 parent class 继承 object[] 类型的 [ProtoMember]
ProtoBuf-Net: Inheriting [ProtoMember] of type object[] from a parent class
object[] mList 包含 child collection 想要的任何 object。
这应该是一个虚拟包装器 class,用于 objects 的其余更指定的 collections。
[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;}
}
}
然后我们有一个上述虚拟包装器的示例 child class,它应该继承所需类型的 object[]。
[ProtoContract]
public class Object1CollectionProto : ObjectCollectionProto
{
public Object1CollectionProto()
{
}
}
如何指定 class 层次结构,以便 Object1CollectionProto 继承 object[] mList 作为可以序列化的 Object1 的列表?就我而言,Object1 已经可以序列化了。只是不是他们的 collection 版本。
How to serialize arrays?
这是解决问题的好方法,只需重新制作框架以适应这种序列化数组的方式。
此外,经过仔细思考,我找到了一种正确序列化 array 的方法。在我的例子中,我 NEVER 实际上序列化了 PARENT class,但我只序列化了 CHILDREN.
我从父 class 数组中删除了 ProtoMember 属性,并在子数组中设置了数组,并将特定类型放入 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; }
}
}
object[] mList 包含 child collection 想要的任何 object。 这应该是一个虚拟包装器 class,用于 objects 的其余更指定的 collections。
[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;}
}
}
然后我们有一个上述虚拟包装器的示例 child class,它应该继承所需类型的 object[]。
[ProtoContract]
public class Object1CollectionProto : ObjectCollectionProto
{
public Object1CollectionProto()
{
}
}
如何指定 class 层次结构,以便 Object1CollectionProto 继承 object[] mList 作为可以序列化的 Object1 的列表?就我而言,Object1 已经可以序列化了。只是不是他们的 collection 版本。
How to serialize arrays? 这是解决问题的好方法,只需重新制作框架以适应这种序列化数组的方式。
此外,经过仔细思考,我找到了一种正确序列化 array 的方法。在我的例子中,我 NEVER 实际上序列化了 PARENT class,但我只序列化了 CHILDREN.
我从父 class 数组中删除了 ProtoMember 属性,并在子数组中设置了数组,并将特定类型放入 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; }
}
}