background preloader

Serialization

Facebook Twitter

Flow - ISerializationSurrogate. DependencyProperty Serialization Part II: ISerializationSurrogate, Reflection Magic and a strange Exception. First off, my apologies for not posting the second part earlier.

DependencyProperty Serialization Part II: ISerializationSurrogate, Reflection Magic and a strange Exception

I have had a lot to do in the past Months… Part II of the article will show how a SerializationSurrogate works and explore wheter we can use it for generic de-serialization of DependencyProperties. Also, in this article I provide full source code for both parts of the article. UPDATE: A much better version of this article can now be found at the CodeProject: DependencyProperty Serialization for Business Objects Alternatively, directly get the source code here: Well, let’s get started: ISerializationSurrogate This interface is designed as a stand-in for your class upon serialization and de-serialization. It is quite simple and contains only two methods: public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) and public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) Surrogate Serialization 03. 05. 06. 08. 14. if (attribs.Length > 0) 22.

Jeffrey Richter

C# Tutorial - Serialize Objects to a File. While storing information in memory is great, there comes a time your users will have to shut your application down.

C# Tutorial - Serialize Objects to a File

This means (probably) that you will need to write information to a file at some point, because you will want to store whatever data was in memory. Today, we are going to take a look at a feature built into .NET called Serialization that makes writing and reading data structures to and from a file extremely easy. For this example, let's say I want to create a program that keeps track of all the cars my friends own. I'm going to create two objects to achieve this: Car and Owner. The Car object will store the make, model, and year of the car. //information about the carpublic class Car{ private string make; private string model; private int year; private Owner owner; public Car() { }} //information about the car's ownerpublic class Owner{ private string firstName; private string lastName; public Owner() { }} List<Car> cars = new List<Car>();

C# Tutorial - XML Serialization. A long while ago we posted a tutorial on how to serialize objects to a binary file.

C# Tutorial - XML Serialization

While this is very useful, unfortunately the resulting file is not very human readable. In this tutorial, I'm going to demonstrate how to serialize your own objects to and from an XML file. Since .NET can use reflection to get property names, basic serialization is unbelievably simple. It only gets slightly difficult when you want to name your XML tags differently than your property names (but still not very hard).

If you've ever used an XML serialization package in C++ like boost, tinyXML, or libXML2, you'll see how comparatively easy C# is to use. Let's start with a basic example. Public class Movie{ public string Title { get; set; } public int Rating { get; set; } public DateTime ReleaseDate { get; set; }} All right, now that we have an object, let's write a function that will save it to XML. After this code executes, we'll have an XML file with the contents of our movie object. <? <? <? <?