序列化回调和AddSubtype在protobuf网问题 [英] Problem with serialization callbacks and AddSubtype in protobuf-net

查看:118
本文介绍了序列化回调和AddSubtype在protobuf网问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 大众A级
  {
    公众诠释X {获得;私定; }
    公开发行A(INT X)
    {
      X = X;
    }

    公共静态隐含的运营商ASurrogate(A一)
    {
      返回== NULL?空:新ASurrogate {X = a.X};
    }
    公共静态隐式操作员A(ASurrogate一)
    {
      返回== NULL?空:新A(a.X);
    }
  }

  [ProtoContract]
  公共抽象类ASurrogateBase
  {
    公共抽象INT X {获得;组; }
  }

  [ProtoContract]
  公共类ASurrogate:ASurrogateBase
  {
    [OnSerializing]
    公共无效OnSerializing(的StreamingContext上下文)
    {
      X = 17;
    }

    [OnDeserialized]
    公共无效OnDeserialized(的StreamingContext上下文)
    {
      X = 117;
    }

    [ProtoMember(1)]
    公众覆盖INT X {获得;组; }
  }

  [ProtoContract]
  大众B级
  {
    [ProtoMember(1)]
    公开发行A A {获得;组; }
  }

  类节目
  {
    静态无效的主要()
    {
      VAR M = RuntimeTypeModel.Default;
      m.AutoCompile = FALSE;
      m.Add(typeof运算(ASurrogateBase),真).AddSubType(1的typeof(ASurrogate)); //(*)
      m.Add(typeof运算(A),FALSE).SetSurrogate(typeof运算(ASurrogate));

      变种B =新的B {A =新的A(117)};
      使用(VAR毫秒=新的MemoryStream())
      {
        Serializer.Serialize(MS,B);
        ms.Position = 0;
        VAR B2 = Serializer.Deserialize< B>(毫秒);
        Debug.Assert的(b.A.X == b2.A.X);
      }
    }
  }
 

这个程序的问题是,无论是序列化回调函数。但是,如果我删除了声明(*),则一切正常。

设计是不是?

解决方案
  

设计是不是?

没有事实并非如此。短的版本是,它没有考虑代理人的继承,(由于各种原因),大部分的回调仅是通过有效的基地继承链的类型调用(除了这里是OnDeserializing,这是在子类调用创建水平对象)。

这是固定在R414

  public class A
  {
    public int X { get; private set; }
    public A(int x)
    {
      X = x;
    }

    public static implicit operator ASurrogate(A a)
    {
      return a == null ? null : new ASurrogate { X = a.X };
    }
    public static implicit operator A(ASurrogate a)
    {
      return a == null ? null : new A(a.X);
    }
  }

  [ProtoContract]
  public abstract class ASurrogateBase
  {
    public abstract int X { get; set; } 
  }

  [ProtoContract]
  public class ASurrogate : ASurrogateBase
  {
    [OnSerializing]
    public void OnSerializing(StreamingContext context)
    {
      X = 17;
    }

    [OnDeserialized]
    public void OnDeserialized(StreamingContext context)
    {
      X = 117;
    }

    [ProtoMember(1)]
    public override int X { get; set; }
  }

  [ProtoContract]
  public class B
  {
    [ProtoMember(1)]
    public A A { get; set; }
  }

  class Program
  {
    static void Main()
    {
      var m = RuntimeTypeModel.Default;
      m.AutoCompile = false;
      m.Add(typeof(ASurrogateBase), true).AddSubType(1, typeof(ASurrogate)); // (*)
      m.Add(typeof(A), false).SetSurrogate(typeof(ASurrogate));

      var b = new B { A = new A(117) };
      using (var ms = new MemoryStream())
      {
        Serializer.Serialize(ms, b);
        ms.Position = 0;
        var b2 = Serializer.Deserialize<B>(ms);
        Debug.Assert(b.A.X == b2.A.X);
      }
    }
  }

The problem with this program is that neither serialization callback is invoked. If, however, I remove the statement (*), then everything works.

Is it by design?

解决方案

Is it by design?

No it isn't. The short version is that it wasn't considering inheritance of surrogates, and (for various reasons) most of the callbacks are only invoked by the effective base type of an inheritance chain (the exception here is OnDeserializing, which is invoked at sub-type levels as objects are created).

This is fixed in r414

这篇关于序列化回调和AddSubtype在protobuf网问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆