Last active
December 10, 2015 00:09
-
-
Save Zaidos/4349248 to your computer and use it in GitHub Desktop.
Serialize. ANYTHING.
This file contains hidden or 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