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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:03:00+00:00 2026-05-31T12:03:00+00:00

I try to serialize a tree which is composed out of either a Craft

  • 0

I try to serialize a tree which is composed out of either a Craft or a Reagent:

[ProtoContract]
[ProtoInclude(1, typeof(Craft))]
[ProtoInclude(2, typeof(Reagent))]
public interface IReagent : IEquatable
{
    [ProtoMember(1)]
    int ItemId { get; set; }
    [ProtoMember(2)]
    int Quantity { get; set; }
}

[ProtoContract] public class Reagent : IReagent { [ProtoMember(1)] public int ItemId { get; set; } [ProtoMember(2)] public int Quantity { get; set; } }

[ProtoContract] public class Craft : IReagent { [ProtoMember(1)] public int Skill { get; set; }

[ProtoMember(2)]
public Profession Profession { get; set; }

[ProtoMember(3)]
public List<IReagent> Reagents { get; set; }

[ProtoMember(4)]
public int ItemId { get; set; }

[ProtoMember(5)]
public int Quantity { get; set; }

}

Serialization code:

public class ProtobufMessageSerializer<T> : ISerializer<T> {
    private readonly ILog logger;

    public ProtobufMessageSerializer(ILog logger)
    {
        this.logger = logger;
    }

    public byte[] Serialize(T entity)
    {
        byte[] message = new byte[0];

        try
        {
            using (var ms = new MemoryStream())
            {
                Serializer.Serialize(ms, entity);
                message = ms.ToByteArray();
            }


            logger.Info(string.Format("Serialized message with a length of: {0}", message.GetHumanReadableSize()));
        }
        catch (Exception ex)
        {
            logger.Warn(ex);
        }


        return message;
    }

    public T Deserialize(byte[] message)
    {
        T result = default(T);

        try
        {
            using (var ms = new MemoryStream(message))
            {
                result = Serializer.Deserialize<T>(ms);
            }

        }
        catch (Exception ex)
        {
            logger.Warn(ex);
        }


        return result;
    }
}

Test code:


[Test]
public void TestSerializer()
{
    var responseSerializer = container.Resolve<ISerializer<IReagent>>();
    var repos = container.Resolve<CraftRepository>();
    var craft = repos.GetAll().First();

    var serializedForm = responseSerializer.Serialize(craft);
    var deserializedForm = responseSerializer.Deserialize(serializedForm);

    Assert.NotNull(deserializedForm);
}

Upon serializing I get:

The type cannot be changed once a serializer has been generated for Bargains.Data.Craft (Bargains.Data.IReagent)

Stacktrace:

at ProtoBuf.Meta.MetaType.ThrowIfFrozen() in C:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 256
   at ProtoBuf.Meta.MetaType.AddSubType(Int32 fieldNumber, Type derivedType) in C:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 70
   at ProtoBuf.Meta.MetaType.ApplyDefaultBehaviour() in C:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 432
   at ProtoBuf.Meta.RuntimeTypeModel.FindOrAddAuto(Type type, Boolean demand, Boolean addWithContractOnly, Boolean addEvenIfAutoDisabled) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 202
   at ProtoBuf.Meta.RuntimeTypeModel.GetKey(Type type, Boolean demand, Boolean getBaseKey) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 369
  • 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-31T12:03:01+00:00Added an answer on May 31, 2026 at 12:03 pm

    Basically, the interface support currently only extends to members; I should make it an error to use the serialzie/deserialize in this way, or make it work! (the latter obviously preferable).

    If you introduce a wrapper such as:

    [ProtoContract]
    public class Wrapper {
        [ProtoMember(1)]
        public IReagent Reagent {get;set;}
    }
    

    and serialize that, then it should work. You will also need to fix the following broken contract:

    [ProtoContract]
    [ProtoInclude(1, typeof(Craft))]
    [ProtoInclude(2, typeof(Reagent))]
    public interface IReagent 
    {
        [ProtoMember(3)] // <==== was 1
        int ItemId { get; set; }
        [ProtoMember(4)] // <==== was 2
        int Quantity { get; set; }
    }
    

    (the numbers cannot be duplicated inside a single type)

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

Sidebar

Related Questions

I need to serialize a Lucene.net Document instance. When I try the following public
What happens if i'll try to serialize an attribute which is static? thanks
When I try to serialize a populated instance of type List<C>() where: public class
I try to serialize a class, defined as following: [DataContract] public class Human {
i try to serialize my checkbox items. first, i want to tell you what
When I try to serialize class with protected members, I get the following errors:
Try to see which cast is faster (not necessary better): new c++ case or
try it out: volatile float bob = -344.0f; unsigned int fred = (unsigned int)bob;
Hi I am trying to serialize an array of objects which are derived from
I try to serialize an instance of my ActiveRecord model to JSON (using render

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.