使用protobuf-net代理有什么问题?

问题描述

| 使用protobuf-net v2观察以下简单代码:
  interface IObject { }

  [ProtoContract]
  class Person : IObject
  {
    [ProtoMember(1)]
    public int Id { get; set; }
    [ProtoMember(2)]
    public string Name { get; set; }
    [ProtoMember(3,AsReference = true)]
    public Address Address { get; set; }
  }
  [ProtoContract]
  class Address : IObject
  {
    [ProtoMember(1)]
    public string Line1 { get; set; }
    [ProtoMember(2)]
    public string Line2 { get; set; }
  }

  class Command
  {
    public List<IObject> Objects { get; set; }
  }

  internal interface ICommandSurrogatePiece
  {
    IEnumerable<IObject> Objects { get; set; }
  }

  [ProtoContract]
  class CommandSurrogatePiece<T> : ICommandSurrogatePiece 
      where T : class,IObject
  {
    [ProtoMember(1)]
    public List<T> Objects { get; set; }

    #region ICommandSurrogatePiece Members

    IEnumerable<IObject> ICommandSurrogatePiece.Objects
    {
      get { return Objects; }
      set { Objects = value as List<T> ?? value.Cast<T>().ToList(); }
    }

    #endregion
  }

  [ProtoContract]
  class CommandSurrogate
  {
    public static implicit operator Command(CommandSurrogate surrogate)
    {
      var objects = surrogate.Pieces.SelectMany(c => c.Objects).ToList();
      return new Command { Objects = objects };
    }
    public static implicit operator CommandSurrogate(Command cmd)
    {
      var pieces = cmd.Objects.GroupBy(o => o.GetType(),o => o,CreateCommandSurrogatePiece).ToList();
      return new CommandSurrogate { Pieces = pieces };
    }

    private static ICommandSurrogatePiece CreateCommandSurrogatePiece(
        Type type,IEnumerable<IObject> objects)
    {
      var piece = (ICommandSurrogatePiece)Activator.CreateInstance(
          typeof(CommandSurrogatePiece<>).MakeGenericType(type));
      piece.Objects = objects;
      return piece;
    }

    [ProtoMember(1,DynamicType = true)]
    public List<ICommandSurrogatePiece> Pieces { get; set; }
  }

  class Program
  {
    static void Main()
    {
      var person = new Person { Id = 12345,Name = \"Fred\",Address = 
          new Address { Line1 = \"Flat 1\",Line2 = \"The Meadows\" } };
      var person2 = new Person { Id = 2345,Name = \"Fred kaka\",Address = 
          new Address { Line1 = \"Flat 12\",Line2 = \"The Meadows kuku\" } };
      var address = 
          new Address { Line1 = \"Flat 2\",Line2 = \"The Meadows Double\" };
      var address2 = 
          new Address { Line1 = \"Flat 2 bubub\",Line2 = \"The Meadows Double kuku\" };

      var model = TypeModel.Create();
      model.Add(typeof(CommandSurrogate),true);
      model.Add(typeof(Command),false).SetSurrogate(typeof(CommandSurrogate));
      var command = new Command { Objects = 
          new List<IObject> { person,address,person2,address2 } };
      var command2 = (Command)(CommandSurrogate)command;
      var command3 = Serializer.DeepClone(command);
    }
  }
最后一行失败,但有异常。我究竟做错了什么? 谢谢。 编辑
System.InvalidOperationException occurred
  Message=Type is not expected,and no contract can be inferred: HelloProtoBuf.Command
  Source=protobuf-net
  StackTrace:
       at ProtoBuf.Meta.TypeModel.ThrowUnexpectedType(Type type)
  InnerException: 
编辑2 我已对代码进行了少许修改以修复代理代码,但这不会影响问题-仍然存在。 编辑3 我知道
command2
command
包含对象的顺序不同。在我的情况下,这是可以接受的。我原以为
command3
等同于
command2
,但是由于某种原因我得到了例外。     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)