Created
June 6, 2012 11:14
-
-
Save Rookian/2881311 to your computer and use it in GitHub Desktop.
Dictionary Serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SerializationExtensions | |
{ | |
public static string Serialize<T>(this T obj) | |
{ | |
var serializer = new DataContractSerializer(obj.GetType()); | |
using (var writer = new StringWriter()) | |
using (var stm = new XmlTextWriter(writer)) | |
{ | |
serializer.WriteObject(stm, obj); | |
return writer.ToString(); | |
} | |
} | |
public static T Deserialize<T>(this string serialized) | |
{ | |
var serializer = new DataContractSerializer(typeof(T)); | |
using (var reader = new StringReader(serialized)) | |
using (var stm = new XmlTextReader(reader)) | |
{ | |
return (T)serializer.ReadObject(stm); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment