Extend Yourself
I tend to have a fairly long list of things I want to investigate in .NET development. Extension methods have been on my list for a long time. I finally got around to looking into them and wish I had done so much sooner. They are a handy little feature available in .NET 3.0 and later.
So what are extension methods? I’m glad you asked. Suppose you want to add some functionality to an existing class but you don’t have the source code. One way is to derive a class and add the new functionality there. That works, but if you are using the base class in lots of places in your application, you have to go to each instance and replace the base class with the derived class. This doesn’t give me a warm, fuzzy feeling and makes me think there must be a better way. This is where extension methods come in. They provide a way to extend the functionality of a class without subclassing.
Let’s see how it works. Suppose you want to be able to log exceptions in your application. It would be nice to be able to write something like
catch (Exception ex)
{
ex.Log();
}
That would be very cool, and as it turns out, is exactly what extension methods do for you.
Declare your extension methods in a common project that will be referenced from any project that needs access to the extension method. Extension methods must be static methods declared in a static class.
using System;
using System.Diagnostics;
namespace Common
{
public static class ExceptionExtensions
{
public static void Log(this Exception ex)
{
Debug.WriteLine(ex.Message +
Environment.NewLine +
ex.StackTrace);
}
public static void Log(this Exception ex, string msg)
{
Debug.WriteLine(msg +
Environment.NewLine +
ex.Message +
Environment.NewLine +
ex.StackTrace);
}
}
}
The strange syntax of the first argument means you want to extend the Exception class (and any class derived from it) with the extension method. The second and later arguments are passed to the extension method when you call it. You are free to put whatever code you want in your extension methods. Log to SQL Server if you want, or use your favorite logger.
Now that you have your extension methods written, you can do this in your application code.
catch (Exception ex)
{
ex.Log();
// or
ex.Log("some more info to log");
}
You even have IntelliSense available and it tells you the method is an extension method.
Now, throw new InvalidOperationException("Don't do that.");
produces the following output with the two overloaded calls to Log.
A first chance exception of type 'System.InvalidOperationException' occurred in Common.dll
Don't do that.
at Common.ViewHelper.RegisterView(IRegionManager regionManager, String regionName, Type viewType) in C:\Users\jbennett\Documents\Visual Studio 2008\Projects\PrismFontViewer\Common\ViewHelper.cs:line 25
some more info to log
Don't do that.
at Common.ViewHelper.RegisterView(IRegionManager regionManager, String regionName, Type viewType) in C:\Users\jbennett\Documents\Visual Studio 2008\Projects\PrismFontViewer\Common\ViewHelper.cs:line 25
That’s all there is to it. Enjoy and extend!