Using .NET 3.0 Extension Methods in ArcObjects

Extension methods are one of the more interesting language enhancements introduced in the version 3.0 release of the .NET Framework. Extension methods allow developers to extend existing data types without modifying the original class code or creating a new class that inherits from the original class. So what exactly does that mean? Well, in short it means that you can now add a method to an existing class without doing anything that changes the original class definition. For example, have you ever wished that you could zoom the map by simply calling a method on the Map class instead of writing a function? Let’s look at how we can add a method named Zoom to the IMap interface.

There are several things to keep in mind when writing an extension method. First, the method must reside in a code module (choose Module from the Templates list when adding a new item to your project). Secondly, you must mark the method as an extension method using the Extension() attribute from the System.Runtime.CompilerServices namespace. Lastly, the first parameter of the method defines the class type you want to extend. Here is the code for our extension method:

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Zoom(ByVal map As IMap, ByVal envelope As IEnvelope)
        Dim activeView As IActiveView = DirectCast(map, IActiveView)
        activeView.Extent = envelope
        activeView.Refresh()
    End Sub

Notice the Extension() attribute has been added to the method definition and that the first parameter to our method is typed as IMap. This tells the compiler that it should bind this function to the IMap interface. When we call the Zoom() method this first parameter will not show up in the Intelli-sense and we will not have to pass in an IMap reference. Our Zoom() method will appear along with all of the other properties and methods on the IMap interface except it will be marked with the Extension tag. Here’s some code that calls our new method:

        Dim mxDocument As IMxDocument = DirectCast(m_application.Document, IMxDocument)
        Dim map As IMap = mxDocument.FocusMap

        Dim activeView As IActiveView = DirectCast(map, IActiveView)
        Dim envelope As IEnvelope = activeView.Extent
        envelope.Expand(0.75, 0.75, True)
        map.Zoom(envelope)

If you’re like me you’re now wondering how this is any better than how you’ve been doing it. I’m not going to try and list all of the pros and cons of using extension methods but I will point out what I think is one major advantage.  Extension methods show up in the Intelli-sense for the classes they extend.  I think this is very important because many times I write a method because I can’t find the method I need on the classes I’m working with.  I’m more likely to find and use an extension method than find a similar method on a helper class that’s located somewhere else.  If you put these methods in a common code library and all of the developers in your organization know to use this library then they should be more likely to see the methods as well.  This should help prevent multiple developers from writing the same functions.

There is still quite a bit more to know about extension methods that I can’t cover here so I’ll just refer you to MSDN with the link below. There is also a sample project you can download that contains the method we just created along with a couple of others.

http://msdn.microsoft.com/en-us/library/bb384936.aspx

Hopefully this article will get you thinking about some of the interesting things you can do to extend the ArcObjects classes but don’t stop there. You can use extension methods to extend any class. So if you’ve ever wished the String class had a few more methods or the Combobox control did something more just write your own methods and add them!

Advertisements

Additions to the GISi Team

GISi is proud to announce the addition of ten new employees to the company:  Ruth Perez, James Blacker, Andrew Martus, Eric Rippons, Adam Kerr, Christopher Fricke, Aaron Van Wieren, Elena Grillo, Casey Hanson and Steve Mulberry. Read more of this post

Which ArcGIS Server API or ADF Should I Choose?

One of the most common questions I receive from clients lately is: “Which API should I use?”  Unfortunately it is not a simple answer; ESRI provides three APIs and two ADFs which include; JavaScript API, Flex API, Silverlight API and the .Net and Java Web.ADFs.  I answer the question with a combination of questions needed to isolate the answer.  For example: Read more of this post