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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:04:30+00:00 2026-05-29T06:04:30+00:00

Is it possible to detect the type of a raw protocol buffer message (in

  • 0

Is it possible to detect the type of a raw protocol buffer message (in byte[])

I have a situation where an endpoint can receive different messages and I need to be able to detect the type before I can deserialize it.

I am using protobuf-net

  • 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-29T06:04:30+00:00Added an answer on May 29, 2026 at 6:04 am

    You can’t detect the type in isolation, since the protobuf spec doesn’t add any data to the stream for this; however, there are a number of ways of making this easy, depending on the context:

    • a union type (as mentioned by Jon) covers a range of scenarios
    • inheritance (protobuf-net specific) can be versatile – you can have a base-message type, and any number of concrete message types
    • you can use a prefix to indicate the incoming type

    the last approach is actually very valuable in the case of raw TCP streams; this is on the wire identical to the union type, but with a different implementation; by deciding in advance that 1=Foo, 2=Bar etc (exactly as you do for the union type approach), you can use SerializeWithLengthPrefix to write (specifying the 1/2/etc as the field number), and the non-generic TryDeserializeWithLengthPrefix to read (this is under Serializer.NonGeneric in the v1 API, or on the TypeModel in the v2 API), you can provide a type-map that resolves the numbers back to types, and hence deserialize the correct type. And to pre-empt the question “why is this useful with TCP streams?” – because: in an ongoing TCP stream you need to use the WithLengthPrefix methods anyway, to avoid over-reading the stream; so you might as well get the type identifier for free!

    summary:

    • union type: easy to implement; only down side is having to then check which of the properties is non-null
    • inheritance: easy to implement; can use polymorphism or discriminator to handle “what now?”
    • type prefix: a bit more fiddly to implement, but allows more flexibility, and has zero overhead on TCP streams
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to detect if a page is opened in 2 different browsers?
I have a problem where I need to detect whether a given type is
Possible Duplicate: Best way to detect integer overflow in C/C++ If I have an
Is it possible to detect object type from CoreData model based on input string?
Is it possible to detect a Nullable type (cast into an object) when it
Is it possible to detect if a class is of certain type?
Using a Java servlet, is it possible to detect the true file type of
Is it possible to detect the HTTP request method (e.g. GET or POST) of
Is it possible to detect when the user clicks on the browser's back button?
Is it possible to detect a valid regular expression with another regular expression? If

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.