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

  • SEARCH
  • Home
  • 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 6041879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:43:31+00:00 2026-05-23T06:43:31+00:00

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

  • 0
  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?

  • 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-23T06:43:31+00:00Added an answer on May 23, 2026 at 6:43 am

    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

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

Sidebar

Related Questions

Assuming public class MyClass { public int ID {get; set; } public string Name
Consider the following class public class Class1 { public int A { get; set;
my code like below public class User { public int ID { get; set;
Say we've got a class like public class Doer { public int Timeout {get;set;}
So I have this model setup public class ListOfThings { public int Id {get;set;}
Say I have this class: public class Account { public int AccountID { get;
I have the following class: public class Account { public int AccountID { get;
I have the following class: public class Day { public int Date { get;
I have this (simplified) class: public class StarBuildParams { public int BaseNo { get;
I have the following class: public class Location { public int Id { get;

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.