<?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>Circles and Crosses &#187; C#</title>
	<atom:link href="http://ox.no/posts/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://ox.no</link>
	<description>Håvard Stranden&#039;s website</description>
	<lastBuildDate>Sun, 22 Apr 2012 22:21:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Problems running .NET 4.0 tests using NUnit 2.5 with (or without) Continuous Testing?</title>
		<link>http://ox.no/posts/problems-running-net-4-0-tests-using-nunit-2-5-with-or-without-continuous-testing</link>
		<comments>http://ox.no/posts/problems-running-net-4-0-tests-using-nunit-2-5-with-or-without-continuous-testing#comments</comments>
		<pubDate>Wed, 11 Aug 2010 09:11:08 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Continuous Testing]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[continuous testing]]></category>
		<category><![CDATA[nunit]]></category>

		<guid isPermaLink="false">http://ox.no/?p=222</guid>
		<description><![CDATA[NUnit 2.5 up to and including 2.5.4 fails to properly detect certain revisions of .NET 4.0, causing it to crash. The issue is documented in this bug report on LaunchPad. The bug has been fixed in 2.5.5 and later, so &#8230; <a href="http://ox.no/posts/problems-running-net-4-0-tests-using-nunit-2-5-with-or-without-continuous-testing">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>NUnit 2.5 up to and including 2.5.4 fails to properly detect certain revisions of .NET 4.0, causing it to crash. The issue is documented in <a href="https://bugs.launchpad.net/nunitv2/+bug/561487">this bug report on LaunchPad</a>. The bug has been fixed in 2.5.5 and later, so <a href="http://www.nunit.org">go grab the latest version of NUnit 2.5</a> and run your tests smoothly again, preferably on each build using <a href="http://ox.no/software/continuoustesting">Continuous Testing</a>, of course. <img src='http://ox.no/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/problems-running-net-4-0-tests-using-nunit-2-5-with-or-without-continuous-testing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocking HtmlHelper in ASP.NET MVC 2 and 3 using Moq</title>
		<link>http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq</link>
		<comments>http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq#comments</comments>
		<pubDate>Wed, 11 Aug 2010 08:51:39 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://ox.no/?p=226</guid>
		<description><![CDATA[Still having trouble mocking HtmlHelper? This is an update to my previous post on mocking HtmlHelper way back when ASP.NET MVC RC1 was released. Eric notified me through a comment on the post and a question on StackOverflow that the &#8230; <a href="http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Still having trouble mocking HtmlHelper? This is an update to <a href="http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq">my previous post on mocking HtmlHelper</a> way back when ASP.NET MVC RC1 was released. Eric notified me through <a href="http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq#comment-840">a comment</a> on the post and a <a href="http://stackoverflow.com/questions/3445770/missingmethodexception-thrown-when-trying-to-mock-htmlhelper-with-moq">question on StackOverflow</a> that the code for ASP.NET MVC RC1 did not work with ASP.NET MVC 2.  The code in this post should work with ASP.NET MVC 2 and ASP.NET MVC 3 Preview 1.</p>

<p><pre class="brush: csharp; ">

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
    Mock&lt;ViewContext&gt; mockViewContext = new Mock&lt;ViewContext&gt;(
        new ControllerContext(
            new Mock&lt;HttpContextBase&gt;().Object,
            new RouteData(),
            new Mock&lt;ControllerBase&gt;().Object),
        new Mock&lt;IView&gt;().Object,
        vd,
        new TempDataDictionary(),
        new Mock&lt;TextWriter&gt;().Object);
    var mockViewDataContainer = new Mock&lt;IViewDataContainer&gt;();
    mockViewDataContainer.Setup(v =&gt; v.ViewData)
        .Returns(vd);
    return new HtmlHelper(mockViewContext.Object,
                            mockViewDataContainer.Object);
}

</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Copyable available on GitHub</title>
		<link>http://ox.no/posts/copyable-available-on-github</link>
		<comments>http://ox.no/posts/copyable-available-on-github#comments</comments>
		<pubDate>Thu, 10 Dec 2009 23:10:29 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[copyable]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://ox.no/?p=144</guid>
		<description><![CDATA[People actually download and use Copyable, and they tend to use it in scenarios I haven&#8217;t used it in. This results in bug reports and patch submissions. So far, these have been given to me by e-mail or by blog &#8230; <a href="http://ox.no/posts/copyable-available-on-github">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>People actually download and use Copyable, and they tend to use it in scenarios I haven&#8217;t used it in. This results in bug reports and patch submissions. So far, these have been given to me by e-mail or by blog comment, neither of which is a particularly great way of receiving them. So after receiving another one today, I finally got around to putting Copyable on <a href="http://github.com">GitHub</a>.</p>

<p>The version I put up includes several enhancements from the latest release:</p>

<ul>
<li>It uses <code>FormatterServices.GetUninitializedObject</code> and hence does not depend on a parameterless constructor or custom instance provider (but you can of course still create an instance provider if you want to control object initialization)</li>
<li>The bug with copy semantics for already visited objects submitted by Walter Oesch has been fixed</li>
<li>The bug with inherited fields found by Alex, and the patch submitted for it, has been incorporated</li>
</ul>

<p>Bleeding edge Copyable can be found at <a href="http://github.com/havard/copyable">http://github.com/havard/copyable</a>. The clone URL is <a href="git://github.com/havard/copyable.git">git://github.com/havard/copyable.git</a>. Now go fix your own bugs! Or even better, enhance the framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/copyable-available-on-github/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Minimalistic MapReduce in .NET 4.0 with the new Task Parallel Library (TPL)</title>
		<link>http://ox.no/posts/minimalistic-mapreduce-in-net-4-0-with-the-new-task-parallel-library-tpl</link>
		<comments>http://ox.no/posts/minimalistic-mapreduce-in-net-4-0-with-the-new-task-parallel-library-tpl#comments</comments>
		<pubDate>Tue, 03 Nov 2009 22:58:52 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[Parallel]]></category>
		<category><![CDATA[TPL]]></category>

		<guid isPermaLink="false">http://ox.no/?p=119</guid>
		<description><![CDATA[Among the news in .NET 4.0 are several additions by the Parallel Computing Platform Team. As I wandered through the documentation of the Task library with cloud computing and parallelism buzz in the back of my head, I got the &#8230; <a href="http://ox.no/posts/minimalistic-mapreduce-in-net-4-0-with-the-new-task-parallel-library-tpl">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Among the news in .NET 4.0 are several additions by the <a href="http://blogs.msdn.com/pfxteam/">Parallel Computing Platform Team</a>. As I wandered through the documentation of the Task library with cloud computing and parallelism buzz in the back of my head, I got the idea of using tasks to create a minimalistic MapReduce. Here&#8217;s the result, a rather crude and simple, but efficient MapReduce for you to play with and utilize!</p>

<!-- more -->

<h2>What is MapReduce?</h2>

<p>For those of you who don&#8217;t know what MapReduce is: MapReduce is a simplified interface for parallel data processing. MapReduce was initially described by the Google engineers Jeffrey Dean and Sanjay Ghemawat in the 2004 paper titled <a href="http://labs.google.com/papers/mapreduce.html">MapReduce: Simplified data processing on large clusters</a>.</p>

<p>MapReduce processes data by splitting the processing in to a set of transformations (in functional programming, this is called the &#8220;map&#8221; function (it maps or transforms an input to an output)). The results of the transformations are then combined into a single result (in functional programming, this is called the &#8220;reduce&#8221; function (it reduces a set of values to a single value)). On a sidenote, Linq has equivalent functions, but the names are different, presumably to make them more familiar to people with SQL knowledge. In Linq, map is called <code>Select</code>, and reduce is called <code>Aggregate</code>.</p>

<p>Shortly put, to process a huge set of data, you split the data into chunks and process each chunk in parallel. This eventually creates a new set of intermediary results, which is reduced to a single result.</p>

<h2>Implementing a minimalistic MapReduce in .NET 4.0</h2>

<p>The signature of my MapReduce function is
<pre class="brush: csharp; ">

static Task&lt;TResult&gt; Start&lt;TInput, TPartial, TResult&gt;(
  Func&lt;TInput, TPartial&gt; map, 
  Func&lt;TPartial[], TResult&gt; reduce, 
  params TInput[] inputs);&lt;/pre&gt;

</pre></p>

<p>In other words, to start a MapReduce run, you supply a <code>map</code> function, a <code>reduce</code> function, and a set of inputs. Each input will be turned into an intermediate result (of type <code>TPartial</code>). Inputs are transformed concurrently. When all inputs are transformed, the <code>reduce</code> function is called to transform the partial results into a final result (of type <code>TResult</code>). Cool!</p>

<p>The map part is implemented by starting a task for each supplied input using <code>Task.Factory.StartNew()</code>.</p>

<p><pre class="brush: csharp; ">

Task.Factory.StartNew(() =&gt; map(input));

</pre></p>

<p>The reduce part is implemented as a <a href="http://en.wikipedia.org/wiki/Continuation">continuation</a> of all the map tasks, meaning that the reduce task waits for all the map tasks to complete, and then executes. This is achieved using <code>Task.Factory.ContinueWhenAll</code>.</p>

<p><pre class="brush: csharp; ">

Task.Factory.ContinueWhenAll(
  mapTasks, 
  tasks =&gt; PerformReduce(reduce, tasks));

</pre></p>

<p>As you can see, the implementation is minimalistic and simple, and usage is likewise.</p>

<p>Here&#8217;s a simple example using MapReduce to calculate the <a href="http://en.wikipedia.org/wiki/Root_mean_square">root mean square (MSE)</a> of a set of values:
<pre class="brush: csharp; ">

var task = MapReduce.Start&lt;int, int, double&gt;(
  i =&gt; i * i,
  s =&gt; Math.Sqrt(s.Aggregate((a, b) =&gt; a + b) / 5),
  1, 2, 3, 4, 5);
// Wait for result
task.Wait();
// Prints 3.3166...
Console.WriteLine(task.Result);

</pre></p>

<p>Actual applications of MapReduce are of course far more interesting than this simple example.</p>

<h2>Applications of MapReduce</h2>

<p>MapReduce can essentially be applied to any problem where you need a number of things to be done in parallel. It can even be applied in cases where you don&#8217;t need a final result. Just return an arbitrary value as the result (or even better, implement a variant of my MapReduce which uses <code>Action&lt;T&gt;</code>).</p>

<p>A few obvious use cases:</p>

<ul>
<li>Distributed search</li>
<li>Distributed sort</li>
<li>Tokenization</li>
<li>Indexing</li>
<li>Log processing</li>
<li>Machine learning</li>
<li>General artificial intelligence</li>
<li>General data mining</li>
<li>Large scale image processing</li>
<li>&#8230;</li>
</ul>

<p>The list goes on and on, these are just a few things off the top of my head.</p>

<p>You can grab the <a href="http://ox.no/files/MapReduce.cs">source code for MapReduce here</a>. Since this is done in .NET 4.0, it requires Visual Studio 2010 Beta 2 or later.</p>

<p>As usual, play around with it, have fun, and let me know if you find it useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/minimalistic-mapreduce-in-net-4-0-with-the-new-task-parallel-library-tpl/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSA using BouncyCastle</title>
		<link>http://ox.no/posts/rsa-using-bouncycastle</link>
		<comments>http://ox.no/posts/rsa-using-bouncycastle#comments</comments>
		<pubDate>Thu, 19 Mar 2009 06:44:51 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[decryption]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[rsa]]></category>

		<guid isPermaLink="false">http://ox.no/?p=80</guid>
		<description><![CDATA[Trying to do RSA using BouncyCastle, but struggling to find your way around the API? In a previous post (see here) I pondered why the RSA implementation in System.Security.Cryptography is restricted to only the most common usage scenarios. I mentioned &#8230; <a href="http://ox.no/posts/rsa-using-bouncycastle">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Trying to do RSA using BouncyCastle, but struggling to find your way around the API? In a previous post (see <a href="/posts/why-cripple-the-net-rsa-implementation">here</a>) I pondered why the RSA implementation in <code>System.Security.Cryptography</code> is restricted to only the most common usage scenarios. I mentioned <a href="http://bouncycastle.org">BouncyCastle</a> as an alternative for those who wanted a more flexible API, but never got around to providing examples where BouncyCastle was used. By request, this post provides usage examples by building a crude and simple, but efficient set of methods for RSA key generation, encryption, and decryption, all built on top of BouncyCastle.</p>

<p><strong>NOTE</strong>: The general cryptographical security of the presented method is beyond the scope of the article. The code presented is not cryptographically secure for large data sets. If you&#8217;re here looking for a way to do cryptographically secure RSA in the general case, you should look into more complicated approaches including padding, blinding, and more sophisticated block cipher modes. Cryptography is a topic undergoing constant research, so stay up to date and be sure to evaluate the strength of your solution for the scenarios in which you apply it.</p>

<!-- more -->

<p>BouncyCastle provides flexibility and control over your encryption approach, which comes at a cost. The BouncyCastle API might be a bit hard to cope with at first, but if you know encryption in general you should be able to find your way around the API without too much effort. This post will be focusing on RSA, since that was my original need, but it should be mentioned that BouncyCastle provides many other asymmetric (and symmetric) algorithms for which the usage is similar to what you find below.</p>

<h2>Creating RSA keys</h2>

<p>Creating RSA keys is a simple task. The method below lets you specify the key size in bits, and creates a key pair for you.</p>

<p><pre class="brush: csharp; ">

public AsymmetricCipherKeyPair GenerateKeys(int keySizeInBits)
{
  RsaKeyPairGenerator r = new RsaKeyPairGenerator();
  r.Init(new KeyGenerationParameters(new SecureRandom(),
    keySizeInBits));
  AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
  return keys;
}

</pre></p>

<p>That&#8217;s all there is to it.</p>

<h2>Encryption</h2>

<p>Now that we have a key pair, we are ready to encrypt and decrypt using RSA. In the example below, we use a key (public or private) to encrypt a byte sequence. To encrypt a string, simply convert the string to a byte array using <code>Encoding.GetBytes</code>.</p>

<p><pre class="brush: csharp; ">

public byte[] Encrypt(byte[] data, AsymmetricKeyParameter key)
{
  RsaEngine e = new RsaEngine();
  e.Init(true, key);&lt;/p&gt;

&lt;p&gt;int blockSize = e.GetInputBlockSize();&lt;/p&gt;

&lt;p&gt;List&lt;byte&gt; output = new List&lt;byte&gt;();&lt;/p&gt;

&lt;p&gt;for (int chunkPosition = 0; chunkPosition &amp;lt; data.Length; 
    chunkPosition += blockSize)
  {
    int chunkSize = Math.Min(blockSize, data.Length - 
      (chunkPosition * blockSize));
    output.AddRange(e.ProcessBlock(data, chunkPosition,
      chunkSize));
  }
  return output.ToArray();
}

</pre></p>

<p>The approach above uses a list to gather output for the sake of simplicity. Note that the RSA engine can only process a limited block size at a time (block size depends on the key size). The approach above processes a data set of an arbitrary size.</p>

<p>The above method does not impose constraints on which key you use for encryption. Use the public key or the private key as you see fit for your solution.</p>

<h2>Decryption</h2>

<p>The <code>Decrypt</code> method is very similar to the <code>Encrypt</code> method:</p>

<p><pre class="brush: csharp; ">

public byte[] Decrypt(byte[] data, AsymmetricKeyParameter key)
{
  RsaEngine e = new RsaEngine();
  e.Init(false, key);&lt;/p&gt;

&lt;p&gt;int blockSize = e.GetInputBlockSize();&lt;/p&gt;

&lt;p&gt;List&lt;byte&gt; output = new List&lt;byte&gt;();&lt;/p&gt;

&lt;p&gt;for (int chunkPosition = 0; chunkPosition &amp;lt; data.Length;
    chunkPosition += blockSize)
  {
    int chunkSize = Math.Min(blockSize, data.Length - 
      (chunkPosition * blockSize));
    output.AddRange(e.ProcessBlock(data, chunkPosition,
      chunkSize));
  }
  return output.ToArray();
}

</pre></p>

<p>Again, it&#8217;s up to you which key you choose to use. If you want to use the common approach, encrypt using a symmetric cipher, hash the data, and sign the hash with your private key using the above <code>Encrypt</code> method. If you want to use another approach like encrypting the actual data using your private key, you are free to do so.</p>

<p>I hope this post helps those of you who want to apply RSA (or any other asymmetric cipher) to more subtle cases than those supported by the .NET framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/rsa-using-bouncycastle/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Mocking HtmlHelper in ASP.NET MVC RC1 using Moq</title>
		<link>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq</link>
		<comments>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq#comments</comments>
		<pubDate>Sun, 08 Mar 2009 17:15:13 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://ox.no/?p=65</guid>
		<description><![CDATA[For those of you trying to mock HtmlHelper, but finding it difficult, here&#8217;s a mock that works in ASP.NET MVC RC1. The ViewDataDictionary that is passed to the HtmlHelper can be empty, or made to contain the data you want &#8230; <a href="http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For those of you trying to mock HtmlHelper, but finding it difficult, here&#8217;s a mock that works in ASP.NET MVC RC1.</p>

<p><span id="more-65"></span></p>

<p>The <code>ViewDataDictionary</code> that is passed to the <code>HtmlHelper</code> can be empty, or made to contain the data you want for your test.</p>

<p><pre class="brush: csharp; ">

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
  var mockViewContext = new Mock&lt;ViewContext&gt;(
    new ControllerContext(
      new Mock&lt;HttpContextBase&gt;().Object,
      new RouteData(),
      new Mock&lt;ControllerBase&gt;().Object),
    new Mock&lt;IView&gt;().Object,
    vd,
    new TempDataDictionary());&lt;/p&gt;

&lt;p&gt;var mockViewDataContainer = new Mock&lt;IViewDataContainer&gt;();
  mockViewDataContainer.Setup(v =&gt; v.ViewData)
    .Returns(vd);&lt;/p&gt;

&lt;p&gt;return new HtmlHelper(mockViewContext.Object, 
    mockViewDataContainer.Object);
}

</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Extension methods for copying or cloning objects</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects</link>
		<comments>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comments</comments>
		<pubDate>Thu, 10 Jan 2008 19:23:32 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[copyable]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects</guid>
		<description><![CDATA[C# 3.0 includes a new feature known as extension methods, and fiddling with it triggered the idea of creating a mechanism for copying or cloning (virtually) any .NET object or graph of objects. The manifestation of that idea has become &#8230; <a href="http://ox.no/posts/extension-methods-for-copying-or-cloning-objects">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>C# 3.0 includes a new feature known as extension methods, and fiddling with it triggered the idea of creating a mechanism for copying or cloning (virtually) any .NET object or graph of objects. The manifestation of that idea has become a rather decent little framework for copying objects. It performs a deep copy as automatically as it possibly can, and provides mechanisms to easily solve many of the cases which cannot be covered automatically. It is great for copying your custom object hierarchies, and saves you the pain of a solution like implementing <code>ICloneable</code> for an entire hierarchy of objects. Click <a href="http://ox.no/software/copyable" title="The Copyable framework">here</a> to grab it now, and read on for a presentation.<!-- more --></p>

<p>Let&#8217;s start off with a few words on extension methods. They are best explained through an example. Let&#8217;s say we want to be able to calculate area given size. Wouldn&#8217;t it be nice to be able to add <code>GetArea</code> to the already existing <code>Size</code> class? Well, let&#8217;s do so!</p>

<p><pre class="brush: csharp; ">
public static class ExtensionMethods
{
  public static int GetArea(this Size size)
  {
    return size.Width * size.Height;
  }
}
</pre>
As you can see, the new syntax simply allows you to tell the compiler that the <code>this</code> of this method is a <code>Size</code>. This means that the method is an extension of the <code>Size</code> class.</p>

<p>As mentioned, I had the idea of extending the very base of the C# class hierarchy (<code>System.Object</code>) with a method for copying or cloning &#8220;any&#8221; object. Obviously, the method cannot automatically copy <em>any</em> object, since it cannot possibly know how to construct an object from an arbitrary class. Hence, a small framework needed to be created. The goals were to:</p>

<ul>
<li>Enable copying of <em>many</em> objects automatically.</li>
<li>Enable copying of virtually <em>any</em> object with very little effort.</li>
<li>Automate and hide away as much as possible (The <a href="http://en.wikipedia.org/wiki/KISS_principle" title="The KISS Principle">KISS</a> Principle).</li>
</ul>

<p>The result is Copyable (pun intended). <!-- more --></p>

<h2>The Copyable framework</h2>

<p>Copyable is a small framework for copying (or cloning, if you will) objects. The straightforward way of using it is to just reference the assembly it&#8217;s in from your project, and start copying!</p>

<p><pre class="brush: csharp; ">
SomeType instance = new SomeType();
// ...do lots of stuff to the object...
SomeType copy = instance.Copy(); // Create a deep copy
</pre></p>

<p>The instance <code>copy</code> is now a deep copy of <code>instance</code>, no matter how complex the object graph for <code>instance</code> is. The relations in the <code>copy</code> graph is the same as in <code>instance</code>, but all objects in the <code>copy</code> object graph are copies of those in <code>instance</code>.</p>

<!--adsense-->

<p>For the automated copy to work, though, one of the following statements must hold for <code>instance</code>:</p>

<ul>
<li>Its type must have a parameterless constructor, or</li>
<li>It must be a <code>Copyable</code>, or</li>
<li>It must have an <code>IInstanceProvider</code> registered for its type.</li>
</ul>

<p>Besides the <code>Copy</code> method, The <code>Copyable</code> class and <code>IInstanceProvider</code> interface are the two major building blocks of the Copyable framework. Each of these blocks enable copying of objects that cannot automatically be copied.</p>

<h2>The Copyable base class</h2>

<p><code>Copyable</code> is an abstract base class for objects that can be copied. To create a copyable class, you simply subclass <code>Copyable</code> and call its constructor with the arguments of your constructor.</p>

<p><pre class="brush: csharp; ">
class MyClass : Copyable
{
  public MyClass(int a, double b, string c)
    : base(a, b, c)
  {
  }
}
</pre></p>

<p>This code above makes <code>MyClass</code> a copyable class. Note that if <code>MyClass</code> had had a parameterless constructor, subclassing <code>Copyable</code> would not be necessary.</p>

<p><code>MyClass</code> can now be copied just like the previous example.
<pre class="brush: csharp; ">
MyClass a = new MyClass(1, 2.0, &quot;3&quot;);
MyClass b = a.Copy();
</pre></p>

<p>The introduction of the <code>Copyable</code> base class solves many problems, but not all. Let&#8217;s say you wanted to copy a <code>System.Drawing.SolidBrush</code>. This class does not have a parameterless constructor, which means it cannot be copied &#8220;automatically&#8221; by the framework. Also, you cannot alter it so that it subclasses <code>Copyable</code>. So, what do you do? You create an instance provider.</p>

<h2>The IInstanceProvider interface</h2>

<p>An instance provider is defined by the interface <code>IInstanceProvider</code>. As the name clearly states, the implementation is a provider of instances. One instance provider can provide instances of one given type. The Copyable framework automatically detects <code>IInstanceProvider</code> implementations in all assembies in its application domain, so all you need to do to create a working instance provider is to define it. No registration or other additional operations are required. To simplify the implementation of instance providers and the <code>IInstanceProvider</code> interface, an abstract class <code>InstanceProvider</code> is included in the framework.</p>

<dl>
<dt><pre class="brush: csharp; ">
public class SolidBrushProvider&lt;/dt&gt;
&lt;dd&gt;InstanceProvider&lt;SolidBrush&gt;
{
public override SolidBrush CreateTypedCopy(SolidBrush s)
{
return new SolidBrush(s.Color);
}
}
</pre></dd>
</dl>

<p>This implementation will be used automatically by the Copyable framework. <strong>NOTE: To be usable, the instance provider MUST have a parameterless constructor.</strong></p>

<p>The instance provider pattern does not solve the case where you want different initial states for your SolidBrush instances depending on which context you use them for copying. For those cases, an overload of <code>Copy()</code> exists which takes an already created instance as an argument. This argument will become the copy.</p>

<p><pre class="brush: csharp; ">
SolidBrush instance = new SolidBrush(Color.Red);
instance.Color = Color.Black;
SolidBrush copy = new SolidBrush(Color.Red);
instance.Copy(copy); // Create a deep copy
</pre></p>

<p>In this example, <code>copy</code> is now of the color <code>Color.Black</code>.</p>

<h2>Limitations and pitfalls</h2>

<p>Although this solution works in most cases, it&#8217;s not a silver bullet. Be aware when you copy classes that hold unmanaged resources such as handles. If these classes are designed on the premise that their resources are exclusive to them, they will manage them as they see fit. Imagine if you copied a class which holds a handle, disposed one of the instances, and continued using the copy. The handle will (probably) be freed by the original instance, and the copy will generate an access violation by attempting reading or writing freed memory.</p>

<p>That&#8217;s it! The Copyable framework can be downloaded from <a href="http://ox.no/software/copyable" title="The Copyable framework">here</a>. For those interested in reading more on extension methods, For additional information, MSDN provides an excellent explanation in the <a href="http://msdn2.microsoft.com/en-us/library/bb383977.aspx" title="Extension methods in C# 3.0">C# Programming Guide</a>, and Scott Guthrie has an introduction article <a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" title="Scott Guthrie on extension methods">here</a>.</p>

<p>Enjoy Copyable, and please let me know if you find it useful or come across any problems with it.</p>

<p><strong>UPDATE 2009-12-11:</strong> Due to popular demand, I have made the source code for Copyable available under the MIT license. The source can be downloaded <a href="http://ox.no/software/copyable" title="The Copyable framework">here</a>.</p>

<p><strong>UPDATE 2010-01-31:</strong> The requirement of parameterless constructors has been removed in the latest version of Copyable available on GitHub. A new release will follow soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/feed</wfw:commentRss>
		<slash:comments>61</slash:comments>
		</item>
	</channel>
</rss>

