0

I'm trying to create a function that will save the current state of my application to a file, and another function to load a saved file. Currently, all the information is contained within a single object, which in turn refers to other objects. I recently heard that C# has some built-in classes that help you serialize and deserialize your objects, so I did a little research and learned about DataContracts, mostly from this page: http://msdn.microsoft.com/en-us/library/ms731073.aspx

Most of it works, except for the classes that implement built-in classes. For example, I have an object that inherits System.Windows.DependencyObject, and when I try to serialize it, it complains that my class inherits a class that does not have the DataContract attribute.

It makes sense to me why that would be a problem. When an object is being deserialized, its constructor is not called. If it inherits something that is not serializable, that might leave it in an invalid state.

I was wondering if this was possible: can I somehow tell the deserializer to call the base class's default constructor before deserializing my object? And then I would have to tell the serializer not to freak out.

4
  • Most serialisation frameworks allow you to ignore (e.g. XmlIgnore) some of your properties during serialisation and let you run custom de-serialisation logic. Commented Mar 14, 2012 at 20:41
  • The problem is that I don't have access to the base class, since it's in the standard C# library. If I had made the base class, it would be easy to make it serializable and tell it what to ignore. Commented Mar 14, 2012 at 20:52
  • When an object is being deserialized, its constructor is not called this is not true. It is called by your serializer thru reflection(not explicitly)(In fact, It is being invoked while creating instance) Commented Mar 14, 2012 at 20:53
  • True. If you are using XmlSerialization I know you need a parameterless constructor, even if its private. Commented Mar 14, 2012 at 20:56

2 Answers 2

1

Can you create a data transer object that has all the properties you want to store, then populate that object with data from the framework object? Mark it as serialized, fire up the serialization class of your choice - and now you have all the info you need. You just need to re-populate the appropriate class after deserialization.

Sign up to request clarification or add additional context in comments.

1 Comment

That's a good idea, but it seems harder than it should be. See my solution below.
0

You may want to look into using a binary serializer or xml serializer instead of a data contract serializer for this one. If you're saving it to a file and don't need the file human-readable binary serialization nearly always works.

See Binary Serialization, and in particular the Basic Serialization topic. Also take a look at the XmlSerializer Class which will sometimes work where a DataContractSerializer doesn't.

2 Comments

That's also a good idea, but it it is important that the output be human-readable.
Ok... in that case I'd try the XmlSerializer and if that doesn't do the trick then you're probably looking at implementing custom serialization by implementing IXmlSerializable on your class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.