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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:22:14+00:00 2026-05-23T08:22:14+00:00

I receive a message using protobuf-net. The message has 1 field with the type

  • 0

I receive a message using protobuf-net. The message has 1 field with the type of the message stored in it (the field is of enum type). Now I know the message is of a type that inherit from the base type. How can I cast the object I get from the Serializer to the appriopriate type?

Definition of classes:

[ProtoContract]
class Annoucement
    {
        public enum msgType
        {
            AKCJA = 0,
            CZEKAJ = 1,
            GOTOWY = 2,
            NOWY_GRACZ = 3,
            LISTA_GRACZY = 4,
            ERROR = 5,
            MAPA = 6,
            UPDATE = 7,
            LISTAGIER = 8,
            JOINGAME = 9,
            QUIT = 10
        }
        [ProtoMember(1)]
        public msgType typ;
    }

[ProtoContract, ProtoInclude(14, typeof(Annoucement))]
    class Update : Annoucement
    {
        [ProtoMember(1)]
        public List<Tank> czolg;
        [ProtoMember(2)]
        public List<Pocisk> pocisk;
        [ProtoMember(3, IsRequired = false)]
        public List<Bonus> bonus;
    }

How can I do something in idea similiar to this:

Annoucement ann = Serializer.DeserializeWithLengthPrefix<Annoucement> (str, PrefixStyle.Base128);
switch (ann.typ) {
case Annoucement.msgType.UPDATE:

                {
                    Update temp = (Update)ann;
                    Console.WriteLine (temp.czolg.Count);
                    List<Tank>.Enumerator i = temp.czolg.GetEnumerator ();
                    Console.WriteLine (i.Current.life);
                }


                break;
  • 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-23T08:22:15+00:00Added an answer on May 23, 2026 at 8:22 am

    It should just work; I believe the problem is that the attributes are inverted (I will take a note to raise a clearer error in this case) – it should be :

    [ProtoContract, ProtoInclude(14, typeof(Update))]
    class Annoucement
    {
    
    }
    
    [ProtoContract]
    class Update : Announcement
    {
    
    }
    

    i.e. the base needs to know about the descendants. Not I removed the discriminator from serialization, as that is redundant if it relates directly to the object type – it handles this internally via the ProtoInclude and will create the correct type for you. Each type needs to know only about the direct subtypes, i.e.

    A
    - B
     - C
    - D
     - E
     - F
    

    here A needs to know about B and D; B needs to know about C; D needs to know about E and F.

    Note that a “what is this” enum is a good idea here, but there is no need for it to be a field – a virtual property without a field may be more appropriate. If I have misundersood and the message-type doesn’t relate to the object-type, then ignore me ;p

    Also: public fields – don’t do it. Think of the kittens… It will work, but properties are preferred (in general, I mean; nothing to do with protobuf-net).

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

Sidebar

Related Questions

I am using Android-C2DM, and when I receive a message that has been pushed
I am using the below code to receive the message via serial port which
I am using thread which will receive messages from the external application.So my thread
I'm using XStream and JETTISON's Stax JSON serializer to send/receive messages to/from JSON javascripts
I currently have designed a system for simple send message receive message using such
I'm currently using Retlang for message-based multithreading in .NET, which is a great library.
I have the need to send/receive messages towards/from different topics stored on a single
When using win32api.setConsoleCtrlHandler() , I'm able to receive shutdown/logoff/etc events from Windows, and cleanly
I am using protobuf now for some weeks, but I still keep getting exceptions
Problem I have a protobuf message definition with a MessageType field, which is an

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.