1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Serializing A Type

  1. Aug 20, 2014 #1
    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.
  2. jcsd
  3. Aug 27, 2014 #2
    I'm sorry you are not finding help at the moment. Is there any additional information you can share with us?
  4. Aug 27, 2014 #3
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted