Inheritance ProtoBuf Net:从父类继承object[]类型的[ProtoMember]
对象[]mList保存子集合希望它保存的任何对象。 对于其他更为指定的对象集合,这应该是一个虚拟包装器类Inheritance ProtoBuf Net:从父类继承object[]类型的[ProtoMember],inheritance,protobuf-net,protocol-buffers,Inheritance,Protobuf Net,Protocol Buffers,对象[]mList保存子集合希望它保存的任何对象。 对于其他更为指定的对象集合,这应该是一个虚拟包装器类 [ProtoInclude(98, typeof(Object1CollectionProto))] [ProtoInclude(99, typeof(Object2CollectionProto))] [ProtoInclude(100, typeof(Object3CollectionProto))] public class ObjectCollectionProto { pro
[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;}
}
}
然后,我们有一个上述虚拟包装器的示例子类,该子类应该继承其所需类型的对象[]
[ProtoContract]
public class Object1CollectionProto : ObjectCollectionProto
{
public Object1CollectionProto()
{
}
}
如何指定类层次结构,以便Object1CollectionProto将对象[]mList继承为可以序列化的Object1列表?在我的案例中,Object1已经可以序列化了。只是不是它们的集合版本。
这是解决问题的一个好方法,只需重新制作框架以适应这种序列化数组的方式
经过仔细思考,我找到了一种方法,可以正确地序列化数组。在我的例子中,从未实际序列化父类,而是只序列化子类
我从父类数组中删除了ProtoMember属性,并在子类中设置了数组,在子类中设置了特定类型
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; }
}
}
这是解决问题的一个好方法,只需重新制作框架以适应这种序列化数组的方式
经过仔细思考,我找到了一种方法,可以正确地序列化数组。在我的例子中,从未实际序列化父类,而是只序列化子类
我从父类数组中删除了ProtoMember属性,并在子类中设置了数组,在子类中设置了特定类型
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[]
就是不起作用。我想不出任何“好”的方法来解决这个问题,但是看看你的数据,我很困惑为什么你不能用List
替换ObjectCollectionProto
…我在另一篇文章中看到了这一点,所以我会这样做!谢谢你,马克<代码>对象[]就是不起作用。我想不出任何“好”的方法来解决这个问题,但是看看你的数据,我很困惑为什么你不能用List
替换ObjectCollectionProto
…我在另一篇文章中看到了这一点,所以我会这样做!谢谢你,马克!