Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:03:26+00:00 2026-05-22T17:03:26+00:00

Observe this simple code, using protobuf-net v2: interface IObject { } [ProtoContract] class Person

  • 0

Observe this simple code, using 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);
    }
  }

The last line fails with an exception. What am I doing wrong?
Thanks.

EDIT

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: 

EDIT2

I have slightly modified the code to fix the surrogate code, but this does not affect the problem – it remains.

EDIT3

I am aware that command2 and command contain objects in different order. This is acceptable in my scenario. I was expecting that command3 be equivalent to command2, but for some reason I get that exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:03:27+00:00Added an answer on May 22, 2026 at 5:03 pm

    Oh, on second glance this is easier than I dared hope. If you are using a custom model, you need to use the methods exposed on model. The v1 API (i.e. Serializer.blah) is now just an indirection to RuntimeTypeModel.Default.

    Try:

    var command3 = (Command)model.DeepClone(command);
    

    Note also that the newly created object may come in as null – in my local repro I added:

    if (cmd == null) return null;
    

    to the operator. When I feel less grotty, I’ll take and look and see if it makes sense for the library to ensure as non-null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this simple code and observing monotonically increasing memory usage. I'm using this
I have this form submit code: Event.observe(window, 'load', init, false); function init() { Event.observe('addressForm',
I am trying to trigger the onScroll event this way using prototype: Event.observe(document, 'scroll',
I have written a simple server - client program with Swing interface using singleton
I observe unmanaged.dll files having a unmanaged.dll.manifest file tagging along. On opening this files
It's simple to observe the documents scroll event but I can't seem to find
I am surprised to observe that mono is faster than .NET. Does anyone know
I'm trying to debug an ASP.NET web application in this environment: Windows Server Standard
This is my first time using IB, but after spending a one or two
Using static class members in a class is a common practice. consider the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.