Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have cloned objects queried from a DataContext using Serialize/Deserialize and submitted the new object to the same DataContext without problems. If there are any attached entities, they will need to be requeried using the IDs before submitting the clone.</p> <pre><code>/// &lt;summary&gt;Used for serializing and de-serializing objects.&lt;/summary&gt; public static class Serializer { /// &lt;summary&gt;Clones an object.&lt;/summary&gt; /// &lt;typeparam name="T"&gt;The type of object to be cloned.&lt;/typeparam&gt; /// &lt;param name="source"&gt;The object to be cloned.&lt;/param&gt; /// &lt;returns&gt;A clone of the specified object.&lt;/returns&gt; public static T Clone&lt;T&gt;(T source) { return Deserialize&lt;T&gt;(Serialize(source)); } /// &lt;summary&gt;Serializes an object as an XML string.&lt;/summary&gt; /// &lt;param name="value"&gt;A System.Object representing the object to be serialized.&lt;/param&gt; /// &lt;returns&gt;A System.String representing an XML representation of the specified object.&lt;/returns&gt; public static string Serialize(object value) { if (value.GetType() == typeof(string)) { return value.ToString(); } StringWriter stringWriter = new StringWriter(); using (XmlWriter writer = XmlWriter.Create(stringWriter)) { DataContractSerializer serializer = new DataContractSerializer(value.GetType()); serializer.WriteObject(writer, value); } return stringWriter.ToString(); } /// &lt;summary&gt;Creates an object from an XML representation of the object.&lt;/summary&gt; /// &lt;typeparam name="T"&gt;The type of object to be created.&lt;/typeparam&gt; /// &lt;param name="serializedValue"&gt;A System.String representing an XML representation of an object.&lt;/param&gt; /// &lt;returns&gt;A new object.&lt;/returns&gt; public static T Deserialize&lt;T&gt;(string serializedValue) { Type type = typeof(T); using (StringReader stringReader = new StringReader(serializedValue)) { using (XmlReader reader = XmlReader.Create(stringReader)) { DataContractSerializer serializer = new DataContractSerializer(type); object deserializedValue = serializer.ReadObject(reader); return (T)deserializedValue; } } } } </code></pre>
 

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