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!
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:
And using it is also simple:
You can load all the types in a given assembly and iterate over them very quickly: