Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this has been answered before, but since I have a very concise way (code) for doing IDictionary serialization with the DataContractSerializer class (used by WCF, but could and should be used anywhere) I couldn't resist contributing it here:</p> <pre><code>public static class SerializationExtensions { public static string Serialize&lt;T&gt;(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&lt;T&gt;(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); } } } </code></pre> <p>This works perfectly in .NET 4 and should also work in .NET 3.5, although I didn't test it yet. </p> <p><strong>UPDATE:</strong> It <strong>doesn't</strong> work in .NET Compact Framework (not even NETCF 3.7 for Windows Phone 7) as the <code>DataContractSerializer</code> is not supported!</p> <p>I did the streaming to string because it was more convenient to me, although I could have introduced a lower-level serialization to Stream and then used it to serialize to strings, but I tend to generalize only when needed (just like premature optimization is evil, so it is premature generalization...)</p> <p>Usage is very simple:</p> <pre><code>// dictionary to serialize to string Dictionary&lt;string, object&gt; myDict = new Dictionary&lt;string, object&gt;(); // add items to the dictionary... myDict.Add(...); // serialization is straight-forward string serialized = myDict.Serialize(); ... // deserialization is just as simple Dictionary&lt;string, object&gt; myDictCopy = serialized.Deserialize&lt;Dictionary&lt;string,object&gt;&gt;(); </code></pre> <p>myDictCopy will be a verbatim copy of myDict. </p> <p>You'll also notice that the generic methods provided will be able to serialize any type (to the best of my knowledge) since it is not limited to IDictionary interfaces, it can be really any generic type T.</p> <p>Hope it helps someone out there!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload