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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:07:41+00:00 2026-05-12T10:07:41+00:00

I have about 20 classes for different messages and this number are growing. Each

  • 0

I have about 20 classes for different messages and this number are growing. Each class has a unique ID, so I can transform the class on a byte[] with my own method of serialization and then transform a byte[] again on my class with this uniqueID.
All my messages are children of a BaseMessage class that already implements the uniqueID generation correctly.

What I want to do is direct find the class of a respective ID without using a Enum to compare.
My problem with the Enum is that Enums are not auto updated with my new IDs every time I create a new message class.

There a way to combine Attributes and Assembly to do this, Like discovering all children of BaseClass and then call for a CustomAtributte?

Thank you!

  • 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-12T10:07:41+00:00Added an answer on May 12, 2026 at 10:07 am

    You’re on the right path – that does sound like a good way to handle it. You’ll want to store the unique ID of the type alongside the serialized value, so you can read the ID before deserialization, to direct the deserializer to the correct type. You could also just store the fully qualified type name instead of using an ID, but this is a fine approach too.

    The attribute is simple enough to create:

    class MessageAttribute : Attribute
    {
        public Guid ID; //assuming you want to use a GUID, could be anything
    }
    

    And using it is also simple:

    [Message(ID = new Guid("..."))]
    class SubMessage : BaseMessage
    {
    }
    

    You can load all the types in a given assembly and iterate over them very quickly:

    List<Type> messageTypes = new List<Type>();
    
    //get the assembly containing our types
    Assembly messageAssembly = Assembly.Load(...);
    
    //get all the types in the assembly
    Type[] types = messageAssembly.GetTypes();
    foreach(Type type in types)
    {
        //make sure we inherit from BaseMessage
        if(type.IsAssignableFrom(typeof(BaseMessage))
        {
            //check to see if the inherited type has a MessageAttribute
            object[] attribs = type.GetCustomAttribtues(typeof(MessageAttribute), true);
            if(attribs.Length > 0)
            {
                messageTypes.Add(type);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm talking about here are nested classes. Essentially, I have two classes that
I am really confused about object relationships! I have two classes Person and Address.
I have about 50 images of 50 x 50 pixel size each. I want
I can use some help for a designing my COMport connection classes. I have
Good day everyone. I have a question about making and using derived classes of
I'm new to programming and have some questions about classes. I'll use Karel as
I have NSMutableArray with different objects in it of different classes. Now I want
How would one go about ordering two different classes? So say there's Class1 and
I have about 10 different APIs. which have absolutely different response structure (all of
I have a helper class in my program which has many static functions used

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.