Serializing Types with Subclasses: A Robust Solution

  • Thread starter Thread starter twoski
  • Start date Start date
  • Tags Tags
    Type
Click For Summary
SUMMARY

This discussion addresses the challenge of serializing subclasses in .NET when the base class is not inherently serializable. The proposed solution involves creating a serializable version of the class and utilizing conversion methods. To handle multiple subclasses, the discussion suggests using reflection to dynamically instantiate the correct subclass during deserialization based on a string identifier. This method is preferred over simpler approaches like using enums, as it offers a more robust solution for distinguishing between subclasses.

PREREQUISITES
  • Understanding of .NET serialization techniques
  • Familiarity with class inheritance and polymorphism
  • Knowledge of reflection in .NET
  • Experience with creating and using conversion methods
NEXT STEPS
  • Research .NET serialization best practices for complex object graphs
  • Learn about implementing reflection in C# for dynamic type instantiation
  • Explore advanced techniques for handling polymorphic serialization
  • Investigate the use of attributes in .NET for custom serialization
USEFUL FOR

Software developers, particularly those working with .NET, who need to implement serialization for complex class hierarchies and require a robust solution for managing subclasses during serialization and deserialization processes.

twoski
Messages
177
Reaction score
2
Let's say i have a class which is not my own code. It is not serializable using the .NET serialization utilities.

In order to save the pertinent data for this class i have created a serializable version of the class and have conversion methods which allow me to essentially convert the class to a serailization friendly class.

This works fine if there is only one class, but what if i have several subclasses deriving from this class?

From the serialization perspective, it would be impossible to distinguish one from another. All of them have more or less the same data. Their type is the only thing that differs.

How can i make a generic method for saving the class type, and then instantiating that specific type when i deserialize?

One way i could do it is save a string/enum that tells me what class to instantiate when i deserialize. This sounds hacky to me though. There must be a better, more robust solution.
 
Physics news on Phys.org
I'm sorry you are not finding help at the moment. Is there any additional information you can share with us?
 
Someone on another forum told me that the solution to this would be using reflection, not sure exactly how it would work though.

I would get the string name of a class (there is some method of doing this, not sure what it is though) and use reflection to instantiate that class using the string name when i deserialize.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K