<?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>Alteridem Consulting &#187; .NET</title>
	<atom:link href="http://www.alteridem.net/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alteridem.net</link>
	<description>Software by Design</description>
	<lastBuildDate>Fri, 02 Sep 2011 14:21:27 +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>.NET Application Settings</title>
		<link>http://www.alteridem.net/2011/09/02/net-application-settings/</link>
		<comments>http://www.alteridem.net/2011/09/02/net-application-settings/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 14:11:58 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tips'n'Tricks]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2011/09/02/net-application-settings/</guid>
		<description><![CDATA[Application Settings are the recommended way to save state between runs of your program. Long gone are the days of using the registry or INI files to save information. The only problem with Application Settings though is that they tend to disappear whenever you release a new version of your application. Fortunately, it is easy [...]]]></description>
			<content:encoded><![CDATA[<p>Application Settings are the recommended way to save state between runs of your program. Long gone are the days of using the registry or INI files to save information. The only problem with Application Settings though is that they tend to disappear whenever you release a new version of your application. Fortunately, it is easy to fix this with some boiler plate code.</p>
<p>First, we need to make sure that our project has a settings file. It is usually under the Properties node of the project. If it is not, right click on the solution and <strong>Add | New Item… | Visual C# Items | General | Settings File</strong> and name it <em>Settings.settings.</em> Next, drag it and drop it in your project’s Properties folder.</p>
<p>Next, you need to add a bool User setting called <strong>UpgradeRequired</strong> and default it to true. Next, at the very beginning of your Main, you check if <strong>UpgradeRequired</strong> is true, and if it is, upgrade the settings, set <strong>UpgradeRequired</strong> to false and save out the settings. This will pull in the settings from previous versions of your application.</p>
<p>Your settings should look like this,</p>
<p><img style="display: inline" title="settings" alt="settings" src="http://www.alteridem.net/wp-content/uploads/2011/09/settings.png" width="416" height="106"></p>
<p>The code should look like this,</p>
<pre name="code" class="c#:nogutter">static void Main( string[] args )
{
   // Upgrade settings if required
   if ( Properties.Settings.Default.UpgradeRequired )
   {
      Properties.Settings.Default.Upgrade();
      Properties.Settings.Default.UpgradeRequired = false;
      Properties.Settings.Default.Save();
   }

   // Do work...

   // At the end of main, save out any changes to settings
   Properties.Settings.Default.Save();
}</pre>
<p>Now you just need to create new settings and use them in your application. Any setting with <strong>Application</strong> scope will be set in your <em>app.config</em> and will be read-only. This is often used for connection strings and other information that is unlikely to change. <strong>User</strong> scoped settings also have their default values in <em>app.config</em>, but any changes are written out to an XML file in the user’s <em>AppData</em> folder.</p>
<p>Also, notice that code gets generated for any setting that you create and you access it through a property on <em>Properties.Settings.Default</em>. You can use any complex type for a setting, including classes in your application, as long as they are <strong>Serializable</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2011/09/02/net-application-settings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I Switched from log4net to NLog</title>
		<link>http://www.alteridem.net/2010/11/04/why-i-switched-from-log4net-to-nlog/</link>
		<comments>http://www.alteridem.net/2010/11/04/why-i-switched-from-log4net-to-nlog/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 20:23:06 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[NLog]]></category>
		<category><![CDATA[log4net]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2010/11/04/why-i-switched-from-log4net-to-nlog/</guid>
		<description><![CDATA[As the people who know me know, I have been a big proponent of log4net over the years. I tried very hard to stick with log4net despite years of inactivity of the project. Over the years, I found, reported and fixed issues, but the patches have never been applied. I have reached out to the [...]]]></description>
			<content:encoded><![CDATA[<p>As the people who know me know, I have been a big <a href="http://www.alteridem.net/2008/01/28/speaking-at-toronto-code-camp/">proponent of log4net</a> over the <a href="http://www.alteridem.net/category/net/log4net/">years</a>. I tried very hard to stick with <a href="http://logging.apache.org/log4net/index.html">log4net</a> despite years of inactivity of the project. Over the years, I found, reported and fixed issues, but the patches have never been applied. I have reached out to the remaining members of the team to get involved and help revive the project, but received few responses. I even applied patches and released an <a href="http://www.alteridem.net/2010/07/09/log4net-udpappender-with-ipv6-on-windows-vista-and-7/">updated version myself</a>.</p>
<p>As I started to use that <a href="http://www.alteridem.net/2010/07/09/log4net-udpappender-with-ipv6-on-windows-vista-and-7/">updated version</a>, I soon discovered that the current source has many bugs and actually fails many of its own test suites. I tried to get the test suites running and submitted more patches for those, but still the project remains dormant. Without the support of at least one member of the team, it is difficult to revive a project without forking it, which I don’t want to do.</p>
<p>I ended up talking with a member of the <a href="http://nhforge.org/">NHibernate</a> team, I learned that they were also moving away from <a href="http://logging.apache.org/log4net/index.html">log4net</a> and were looking at <a href="http://nlog-project.org/">NLog</a>, the new player in .NET logging. I read through the website, followed the forums and liked what I saw, so I decided to move over to <a href="http://nlog-project.org/">NLog</a>.</p>
<p>Switching over actually turned out to be fairly straight-forward once I managed a few regular expressions for search and replace. Roughly, I performed the following steps,</p>
<ol>
<li>I did a search and replace using <a href="http://notepad-plus-plus.org/">Notepad++</a> in all of my project files to switch the references over.</li>
<li>I then replaced all of my using statements.</li>
<li>Next came the log instantiation. log4net uses <strong>ILog</strong> where NLog uses <strong>Logger</strong>. In log4net you also tend to use <strong>GetLogger( typeof( MyClass ) )</strong> where in NLog you just use <strong>GetCurrentClassLogger()</strong>. This required a bit of RegEx magic.</li>
<li>The hardest step was finding every instance of <strong>log.Warn( msg, exception)</strong> and converting them to <strong>log.WarnException( msg, Exception )</strong>. I did this nearly manually for each of the log levels, inspecting to see where I was passing in exceptions.</li>
<li>Then for each of the log levels, I converted instances of <strong>log.WarnFormat( &quot;Substitute this {0}&quot;, val )</strong> to <strong>log.Warn( &quot;Substitute this {0}&quot;, val )</strong>. This was another easy file replacement.</li>
<li>Lastly, I removed my <strong>XmlConfigurator</strong> attributes and changed my log configurations. A quick recompile, a few minor fixes and I was up and running. Painless!</li>
</ol>
<p>So far, I don’t regret my choice at all. I find the configuration of NLog to be much more intuitive and the Intellisense for the config file in Visual Studio sure helps. The documentation is still a bit sparse in places, but it is getting better. I do worry because it is currently a one man show, but it <a href="http://jkowalski.com/">Jaroslaw Kowalski</a> does seem fairly active and committed. Time will tell…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2010/11/04/why-i-switched-from-log4net-to-nlog/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>log4net UdpAppender with IPv6 on Windows Vista and 7</title>
		<link>http://www.alteridem.net/2010/07/09/log4net-udpappender-with-ipv6-on-windows-vista-and-7/</link>
		<comments>http://www.alteridem.net/2010/07/09/log4net-udpappender-with-ipv6-on-windows-vista-and-7/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 17:31:40 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[log4net]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2010/07/09/log4net-udpappender-with-ipv6-on-windows-vista-and-7/</guid>
		<description><![CDATA[log4net is a great logging framework for .NET, but it hasn’t been updated in years. When we moved to Windows Vista, we noticed that the UdpAppender stopped working with Chainsaw and with our internal logging tools when logging to localhost. After some Googling, we discovered that replacing localhost with 127.0.0.2 got everything working and we [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://logging.apache.org/log4net/index.html">log4net</a> is a great logging framework for .NET, but it hasn’t been updated in years. When we moved to Windows Vista, we noticed that the <a href="http://logging.apache.org/log4net/release/sdk/log4net.Appender.UdpAppender.html">UdpAppender</a> stopped working with <a href="http://logging.apache.org/chainsaw/index.html">Chainsaw</a> and with our internal logging tools when logging to <strong>localhost</strong>. After some Googling, we discovered that replacing <strong>localhost</strong> with <strong>127.0.0.2</strong> got everything working and we promptly forgot about it.</p>
<p>Earlier this week, we moved one of our projects over to .NET 4.0 and once again logging failed. In the console window while debugging the app, I noticed the following,</p>
<pre>log4net.Util.TypeConverters.ConversionNotSupportedException: Cannot convert from type [System.String]
  value [127.0.0.2] to type [System.Net.IPAddress] ---&gt;
  System.Net.Sockets.SocketException: No such host is known
       at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
       at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
       at log4net.Util.TypeConverters.IPAddressConverter.ConvertFrom(Object source)</pre>
<p>So, I tried changing back to <strong>localhost</strong> and the error changed to,</p>
<pre>log4net:ERROR [UdpAppender] Unable to send logging event to remote host ::1 on port 7071.
      System.Net.Sockets.SocketException (0x80004005): An address incompatible with
      the requested protocol was used
       at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags
         socketFlags, EndPoint remoteEP)
       at System.Net.Sockets.UdpClient.Send(Byte[] dgram, Int32 bytes, IPEndPoint endPoint)
       at log4net.Appender.UdpAppender.Append(LoggingEvent loggingEvent)</pre>
<p>At this point, I have some clues, specifically that localhost was being resolved as the IPv6 address ::1, not the IPv4 address 127.0.0.1. Looking through the reported issues I found that this problem had been <a href="https://issues.apache.org/jira/browse/LOG4NET-112">reported and fixed in 2007</a>. Unfortunately, that code isn’t in the release, so I downloaded the latest source and recompiled log4net. Of course, they don’t release the signing key, so I generated my own and signed the assembly myself.</p>
<p>This fixed my problems with the UdpAppender, although, if you use <strong>localhost</strong> on a Windows Vista or Windows 7 machine, it will resolve to the IPv6 address, so make sure that your receiver is listening on the IPv6 address. For example, in <a href="http://log2console.codeplex.com/">log2console,</a> under Receivers, set Use IPv6 Addresses to true for the UDP receiver. If your receiver does not support IPv6, use <strong>127.0.0.2</strong> for the address.</p>
<p>To save other people the hassle of recompiling log4net themselves to get the latest fixes, I have uploaded a release version of the assembly. Download it here.</p>
<ul>
<li><a href="http://www.alteridem.net/wp-content/uploads/2010/07/log4net_1.2.10.1.zip">log4net 1.2.10.1</a> -  260 KB Zip</li>
</ul>
<p>We are using this version of the assembly in our own projects, but I make no guarantees as to its stability.</p>
<p><strong>Update:</strong> Just to  be clear, this is not an official log4net release and it is only compiled against .NET 2.0. I have made no code changes, it is just the code that is currently in the repository. This is only intended to save you having to download and compile if you run into the same problems I did. It is also not extensively tested. We are using it and File, Rolling File, Event Log and UDP Appenders seem to be working fine.</p>
<p><strong>Update 2:</strong> Since I have released this, I have found several bugs in the current source. For example, setting Append=true to a File or Rolling File causes every log message to truncate the file! Because of that <span style="color: #ff0000;"><strong>I would recommend that you don&#8217;t download this</strong></span> unless you really need it. I have finally <a href="http://www.alteridem.net/2010/11/04/why-i-switched-from-log4net-to-nlog/">given up on log4net</a> and converted all of my projects over to <a href="http://nlog-project.org/">NLog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2010/07/09/log4net-udpappender-with-ipv6-on-windows-vista-and-7/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Extending CopyHelper using Extension Methods</title>
		<link>http://www.alteridem.net/2008/07/22/extending-copyhelper-using-extension-methods/</link>
		<comments>http://www.alteridem.net/2008/07/22/extending-copyhelper-using-extension-methods/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 13:14:00 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Tips'n'Tricks]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/?p=55</guid>
		<description><![CDATA[In my last two posts, I have been developing a small utility library to do the grunt work of copying data from an instance of one class to an instance of another type. The Copier class from my last post allows me to copy all public properties from one class to another class as long [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/">last</a> <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/">two</a> posts, I have been developing a small utility library to do the grunt work of copying data from an instance of one class to an instance of another type. The <a href="http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/"><strong>Copier</strong></a> class from my last post allows me to copy all public properties from one class to another class as long as the properties have the same name and type. All that is done with one line of code;</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:364d24c2-3f44-49d3-b077-d600fcaece3c" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">Copier&lt;ICustomer&gt;.Copy( customer ).To&lt;ICustomerView&gt;( view );</pre>
</div>
<p>Today, I am going to use extension methods to simplify the above code even further. I want to be able to write</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:37699e1e-1b46-4f94-a84f-25266f64cba3" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">customer.CopyTo&lt;ICustomerView&gt;( view );</pre>
</div>
<p>or if we want to rely on type inferencing with the generic <strong>CopyTo</strong> method, you could write it as simply as</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:9c627046-fca3-4dc8-8ad8-df7c27291361" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">customer.CopyTo( view );</pre>
</div>
<p>How is this done? Using extension methods, it was actually much simpler than yesterday’s <a href="http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/"><strong>Copier</strong></a> class. In fact, it just ended up being one line of code for the <strong>CopyTo</strong> method and for the <strong>CopyFrom</strong> method. I simply wrapped the <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/"><strong>CopyHelper</strong></a> class like this.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:d57cca66-24bc-4e64-a84a-3b52c1c5ebb6" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">public static class CopierExtensions
{
    public static void CopyTo&lt;T&gt;( this object from, T to ) where T : class
    {
        CopyHelper.Copy( from.GetType(), from, typeof( T ), to );
    }

    public static void CopyFrom&lt;T&gt;( this object to, T from ) where T : class
    {
        CopyHelper.Copy( typeof( T ), from, to.GetType(), to );
    }
}</pre>
</div>
<p>The only problem I have with this is that the class that the extension methods are applied to are not constrained by an interface, so all matching properties are copied. Does anyone have any ideas on that?</p>
<p>I have uploaded a copy of <a href="http://www.alteridem.net/wp-content/uploads/2008/07/modelviewhelpers.zip">the solution for this project</a> along with an <a href="http://www.nunit.org">NUnit</a> test project. Take a look, use it if you like and feel free to give me suggestions for improvements.</p>
<p>In the next few posts I was thinking of extending these classes even further. I might add attributes that allow you to ignore certain properties, maybe add an attribute that specifies interfaces that you are allowed to copy to, possibly an attribute that allows properties to automatically be converted between types. What would you find useful or like to see? Would you like to see a post on the performance using these methods?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2008/07/22/extending-copyhelper-using-extension-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extending CopyHelper using Generics</title>
		<link>http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/</link>
		<comments>http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 15:33:55 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Tips'n'Tricks]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/</guid>
		<description><![CDATA[In my last post, I created a method that does the grunt work of copying data from an instance of one class to an instance of another type. I often find myself copying data between the properties of my data layer classes and those of my user interface like this. // Copy the data from [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/">last post</a>, I created a method that does the grunt work of copying data from an instance of one class to an instance of another type. I often find myself copying data between the properties of my data layer classes and those of my user interface like this.</p>
</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:d56ee549-681a-47e3-98f2-b8699e660de1" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view
view.Address = customer.Address;
view.Country = customer.Country;
view.FirstName = customer.FirstName;
view.LastName = customer.LastName;
view.PostalCode = customer.PostalCode;
view.Province = customer.Province;</pre>
</div>
<p>The newly created <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/"><strong>CopyHelper</strong> class</a> allows me to shorten that to this.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:106cd999-9de2-4404-bfe0-d08445cb28ca" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view (using reflection in .NET 1.x)
CopyHelper.Copy( typeof(ICustomer), customer, typeof(ICustomerView), view );</pre>
</div>
<p>Today, I want to extend that code using Generics and a <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a> so that I can write code like this.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:21d2f05d-c92d-4fc4-a163-9687494dddde" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view (using reflection and generics in .NET 2.0)
Copier&lt;ICustomer&gt;.Copy( customer ).To&lt;ICustomerView&gt;( view );</pre>
</div>
</p>
<p>Internally, I use my <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/"><strong>CopyHelper</strong> class</a> from my last post. I extend that by creating a generic <strong>Copier</strong> class. I make the constructor private so that it can only be created as a part of the fluent interface, in this case the static copy method. Using the instance of the <strong>Copier</strong> class that was returned from that method, you can then copy <strong>To</strong> or <strong>From</strong> another class instance.</p>
<p>Here is the code.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:e4d4e61e-e527-494e-b96d-0d4b4f9ba5ea" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">public sealed class Copier&lt;T1&gt; where T1 : class
{
    #region Private Members

    private readonly T1 _subject;

    #endregion

    #region Public Interface

    /// &lt;summary&gt;
    /// Begin the copying process.
    /// &lt;/summary&gt;
    /// &lt;param name="interface1"&gt;The object you are copying from or to&lt;/param&gt;
    /// &lt;returns&gt;An instance of the Copier class so that you can
    /// continue with the copy to/from in a fluent interface.&lt;/returns&gt;
    public static Copier&lt;T1&gt; Copy( T1 interface1 )
    {
        return new Copier&lt;T1&gt;( interface1 );
    }

    #endregion

    #region Construction

    /// &lt;summary&gt;
    /// Private constructor so that it can only be created as a part of a fluent interface.
    /// &lt;/summary&gt;
    /// &lt;param name="subject"&gt;&lt;/param&gt;
    private Copier( T1 subject )
    {
        _subject = subject;
    }

    #endregion

    #region Copier Methods

    /// &lt;summary&gt;
    /// Copies properties from the subject to the passed in object.
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T2"&gt;The type of object you are copying into.&lt;/typeparam&gt;
    /// &lt;param name="to"&gt;The object to copy into.&lt;/param&gt;
    /// &lt;returns&gt;The modified object that you passed in.&lt;/returns&gt;
    public T2 To&lt;T2&gt;( T2 to ) where T2 : class
    {
        CopyHelper.Copy( typeof( T1 ), _subject, typeof( T2 ), to );
        return to;
    }

    /// &lt;summary&gt;
    /// Copies properties from the passed in object into the subject.
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T2"&gt;The type of object you are copying from.&lt;/typeparam&gt;
    /// &lt;param name="from"&gt;The object to copy from.&lt;/param&gt;
    /// &lt;returns&gt;The modified subject that you originally passed in the Copy method.&lt;/returns&gt;
    public T1 From&lt;T2&gt;( T2 from ) where T2 : class
    {
        CopyHelper.Copy( typeof( T2 ), from, typeof( T1 ), _subject );
        return _subject;
    }

    #endregion
}</pre>
</div>
</p>
<p>I would like to constrain <strong>T1</strong> and <strong>T2</strong> to interfaces at compile time, but I am not sure if that can be done. If you have ideas, please post them in the comments. I thought of using reflection to check if <strong>T1</strong> and <strong>T2</strong> are interfaces at run time, but I am a big believer in favouring compile time errors over run time errors.</p>
<p>In my <a href="http://www.alteridem.net/2008/07/22/extending-copyhelper-using-extension-methods/">next post</a>, I am going to use C# 3.0 extension methods to further simplify copying allowing you to write code like this.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:acf0083b-b819-41ac-801c-41c0f977934e" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view (using extension methods in C# 3.0)
customer.CopyTo&lt;ICustomerView&gt;( view );</pre>
</div>
<p>I am very interested in hearing your feedback on this, so be sure to post to the comments. Do you think this is a good idea? Do you have suggestions for improvements? Let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

