English 中文(简体)
摘要/记录
原标题:Abstract/array issues with protobuf-net

我在试图淡化来自同一<基类和带的系列物体时遇到困难。

    [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的奥贝亚拉伊方案,我获得以下豁免:

  1. [ProtoMember(1,AsReference=true,DynamicType=true)] (as written above): Argument Exception at System.RuntimeType.TryChangeType. This occurs even if Base is not abstract
  2. [ProtoMember(1,DynamicType=true)] : Argument Exception at System.RuntimeType.TryChangeType. This occurs even if Base is not abstract
  3. [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.
  4. [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!

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...