我在试图淡化来自同一<的基类和带的系列物体时遇到困难。
[ProtoContract]
[ProtoInclude(1,typeof(A))]
[ProtoInclude(2, typeof(B))]
public abstract class Base
{}
[ProtoContract]
public class A:Base
{
[ProtoMember(1)]
public int J
{
get;
set;
}
}
[ProtoContract]
public class B : Base
{ }
[ProtoContract]
public class Obj
{
[ProtoMember(1,AsReference=true,DynamicType=true)]
public Base[] array = new Base[] { new A(), new B() };
}
之后,试图去除/照相:
Obj bob = new Obj();
using (MemoryStream m = new MemoryStream())
{
Serializer.Serialize(m, bob);
m.Position = 0;
var clone = Serializer.Deserialize<Obj>(m);
}
根据Proto Member的奥贝亚拉伊方案,我获得以下豁免:
- [ProtoMember(1,AsReference=true,DynamicType=true)] (as written above): Argument Exception at System.RuntimeType.TryChangeType. This occurs even if Base is not abstract
- [ProtoMember(1,DynamicType=true)] : Argument Exception at System.RuntimeType.TryChangeType. This occurs even if Base is not abstract
- [ProtoMember(1,AsReference=true)] : Cannot create an abstract class exception. If Base is not abstract, I get an "Object does not match target type." exception.
- [ProtoMember(1)] : No exception, but I need to serialise things as a reference (also, the deserialised Obj has FOUR items in array , but I think this bug has already been reported)
I need to be able to serialise the array using AsReference... If I m not doing something wrong, can anyone think of a decent workaround for this? Thanks!