Created
November 29, 2012 09:31
-
-
Save posaunehm/4167810 to your computer and use it in GitHub Desktop.
Extention method for serialize using DataContract
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 DataContractExtention | |
{ | |
public static string ToStringUsingDataContract<T>(this T source) | |
{ | |
var selializer = new DataContractSerializer(source.GetType()); | |
var ms = new MemoryStream(); | |
selializer.WriteObject(ms, source); | |
return Encoding.UTF8.GetString(ms.ToArray()); | |
} | |
public static T ToObjectUsingDataContract<T>(this string source) | |
{ | |
var selializer = new DataContractSerializer(typeof(T)); | |
var ms = new MemoryStream(Encoding.UTF8.GetBytes(source)); | |
return (T)selializer.ReadObject(ms) ; | |
} | |
} |
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
using DataContractExtention | |
//Serialize | |
var x = new SomeClass(); | |
var str = x.ToStringUsingDataContract(); | |
//Deserialzie | |
SomeClass y = str.ToObjectUsingDataContract<SomeClass>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment