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 8286423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:45:52+00:00 2026-06-08T11:45:52+00:00

Is it possible to deserialize an aggregate type that has a property to an

  • 0

Is it possible to deserialize an aggregate type that has a property to an abstract base type that is a reference, see Aggregate.Base? If not, what is the best workaround?

[ProtoContract]
[ProtoInclude(1, typeof(Derived))]
public abstract class Base { }

[ProtoContract]
public class Derived : Base
{
    [ProtoMember(1)]
    public int SomeProperty { get; set; }
}

[ProtoContract]
public class Aggregate
{
    [ProtoMember(1, AsReference = true)]
    public Base Base { get; set; }
}

[TestClass]
public class UnitTest
{
    [TestMethod]
    public void TestMethod1()
    {
        var value = new Aggregate { Base = new Derived() };
        using (var stream = new MemoryStream())
        {
            Serializer.Serialize(stream, value);
            stream.Position = 0;

            // Raises an exception
            // Unable to create type Sage.Estimating.Data.Base: Cannot create an abstract class.
            Serializer.Deserialize<Aggregate>(stream);
        }
    }
}

Call stack at the point the exception is raised:

protobuf-net.dll!ProtoBuf.BclHelpers.ReadNetObject(object value, ProtoBuf.ProtoReader source, int key, System.Type type, ProtoBuf.BclHelpers.NetObjectOptions options) Line 428 + 0xda bytes C#
protobuf-net.dll!ProtoBuf.Serializers.NetObjectSerializer.Read(object value, ProtoBuf.ProtoReader source) Line 45 + 0x9f bytes C#
protobuf-net.dll!ProtoBuf.Serializers.TagDecorator.Read(object value, ProtoBuf.ProtoReader source) Line 66 + 0x18 bytes C#
protobuf-net.dll!ProtoBuf.Serializers.PropertyDecorator.Read(object value, ProtoBuf.ProtoReader source) Line 74 + 0x18 bytes C#
protobuf-net.dll!ProtoBuf.Serializers.TypeSerializer.Read(object value, ProtoBuf.ProtoReader source) Line 205 + 0xf bytes C#
protobuf-net.dll!ProtoBuf.Meta.RuntimeTypeModel.Deserialize(int key, object value, ProtoBuf.ProtoReader source) Line 562 + 0xf bytes C#
protobuf-net.dll!ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoBuf.ProtoReader reader, System.Type type, object value, bool noAutoCreate) Line 634 + 0x14 bytes C#
protobuf-net.dll!ProtoBuf.Meta.TypeModel.Deserialize(System.IO.Stream source, object value, System.Type type, ProtoBuf.SerializationContext context) Line 555 + 0x14 bytes C#
protobuf-net.dll!ProtoBuf.Meta.TypeModel.Deserialize(System.IO.Stream source, object value, System.Type type) Line 534 + 0x13 bytes C#
protobuf-net.dll!ProtoBuf.Serializer.Deserialize(System.IO.Stream source) Line 78 + 0x5a bytes C#

  • 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-06-08T11:45:56+00:00Added an answer on June 8, 2026 at 11:45 am

    Thank you for an excellent scenario; not sure how I overlooked that. Basically, it comes down to key tracking, which gets particularly complex during cyclic graphs. In order to get the key registered ASAP, what it used to do was (for new objects):

    • create an instance of the object
    • register it against the known key
    • deserialize the payload into the newly created object

    Obviously the first step is an error in the case of inheritance, regardless of whether the base-type is abstract / non-creatable. What it does now is:

    • register a dummy (non-fetchable) value against the known key
    • deserialize the payload starting from null
    • trap the object (update the dummy) as it gets created (this code pretty much already existed, although it was specialised for the “root object” scenario; now made more generic)

    The upshot of this is: it now works; your test passes, and the object is of the correct type:

    var obj = Serializer.Deserialize<Aggregate>(stream);
    Assert.AreEqual(typeof(Derived), obj.Base.GetType());
    

    Needs revision 556 or later.

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

Sidebar

Related Questions

Using Protobuf-Net, I see that it does not seem possible to deserialize a class
I have an object to deserialize but the object has custom type ApplicationLanguage that
Possible Duplicate: How to Deserialize XML document Suppose that I have a class that
Is it possible that DataContractSerializer wrongly deserializes an object if the fields are not
It is my understanding that it is not possible to have an optional parameter
I am trying to deserialize a JSON string that looks like so: {'type':'clientlist','client_list':[]} I
It seems that serializing Entity Framework objects into JSON is not possible using either
Is it possible to Deserialize the following piece of XML into a Dictionary<int,string> object?
Is it possible to deserialize an enum from an int in c#. e.g. If
Is it possible to override the default WCF DataContractSerializer behaviour when Serialize/DeSerialize entities and

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.