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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:08:14+00:00 2026-06-05T12:08:14+00:00

I am writing a message processor. The processor receives messages through a stream of

  • 0

I am writing a message processor. The processor receives messages through a stream of data. Messages may have different types, but all types have some common properties, for example length, check-sum, etc. Moreover, each type of message has its own properties as well.

For its implementation, I am thinking of making a class (say message class) that includes common properties, and inheritance from the message class to create individual classes for each message type. First, I am wondering if inheritance is a good solution in this case? or is there any other better way for it?

Also, I would like to pass a raw message to a method and that method returns me the corresponding properties to message (including common and individual). As individual properties change based on the message type, how can I implement the method to return me only one object? To better explain, I do not want it to return a different object for each message type. I would rather have a general object that has some specific properties based on another property like “MessageType”, something like below:

Message Class:
--- Length
--- Check-sum
--- MessageType
--- Property A
--- Property B
     .
     .
     .
--- Property Z

Properties A to Z are individual properties and their number and type could be different based on “MessageType” Method.

Thanks in advance.

  • 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-05T12:08:16+00:00Added an answer on June 5, 2026 at 12:08 pm

    When you say “I do not want it to return a different object for each message type”, do you mean run time, or design time type? If you mean design time type, then that isn’t an issue, just have the method return the base object for all your messages.

    If you do want to return just a single type at run time, then Eric J.’s answer has a good solution for that.

    If you don’t like the idea of using a dictionary of sorts, and you’re using .NET 4.0. You can go the dynamic route. You’d still be using a dictionary, but the code accessing the message object wouldn’t look like a dictionary.

    public class Message : System.Dynamic.DynamicObject
    {
        public Message(object data) { //parse data into defined properties and dictionary}
    
        public string MessageType {get;set;}
        //other "common" properties
    
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
           //binder.Name will hold the property name that is being accessed
           //if the value exists in the message, set it to the result parameter
           //and return true, else set it to null and return false (which will cause an Exception)
        }
    
    }
    

    Calling code:

    dynamic message = new Message(data);
    var type = message.MessageType;
    var customProperty = message.Length;
    

    The call to message.MessageType will simply access the property that is defined.
    The call to Message.Length will invoke the TryGetMember method since no property by that name is defined.

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

Sidebar

Related Questions

I'm writing some if/elsif statements to display error messages. When I leave the username
I'm writing a chat and i would like to have the incoming messages on
Ok, so, I got the signal through but for some reason the process exists
I have a C# service which listens to a queue for XML messages, receives
I am writing a process that will run continuously, getting messages on a queue
I am writing a message transfer program between multiple clients and server. I want
So I am writing a simple inbox private message system. My table schema is
i am writing a code to handle read/unread messages, with a simple user_id/message_id mysql
I am writing a usenet newsreader, and calling the XOVER id- message to get
I'm writing a WinForms application that sends email messages (like a mail merge). I'd

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.