Using C# 3.0 Extension Methods to make life easier when using optional parameters in Office PIAs

Gabhan Berry (read his blog - he has lots of Excel programmability related information in there!) wrote a piece about how to make using optional parameters a little less painful when using C# to program against the Excel (and any other) COM PIA object model.

While his solution makes life much easier in some scenarios, after reading the post, there were a few things that bothered me from a fragility of code point of view about his solution - specifically, the various issues with type safety (parameters and method names). While the mechanism he presents will work properly, I was thinking if I could come up with something that will not have those issues. I think I was able to come up with something, but it has it's own set of issues. The biggest one is the fact that it only works with C# 3.0 - not with 2.0. That said, here's the solution:

OverGen is a tool I wrote that will take an assembly and find all methods in it that are optional and then generate overloads for all those methods that will allow the user to pass in a smaller amount of parameters. For each optional parameter a method has, an extension method will be generated.

Extension Methods are basically a very simple compiler construct that allows a class to extend another class by way of adding methods to it - the extension is very superficial - no methods are actually added to the class but rather, the compiler knows that when such a method is called on class, it will try and find it in supporting classes.

Here's an example of one of the methods generated by the tool:

public static Microsoft.Office.Interop.Excel.Workbook Open(this Microsoft.Office.Interop.Excel.Workbooks @__thisParam,

      string Filename) {

    return @__thisParam.Open(

            Filename,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

      System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing,

            System.Type.Missing);

}

 

Notice the "this" keyword on the first parameter? That's what makes this method an extension method. If you did not have this guy, this is what ANY call to .Open would have to look like, even if you only use the first parameter:

app.Workbooks.Open("c:\\temp\\file.xlsx", Type.Missing, Type.Missing,

      Type.Missing, Type.Missing, Type.Missing, Type.Missing,

      Type.Missing, Type.Missing, Type.Missing, Type.Missing,

      Type.Missing, Type.Missing, Type.Missing, Type.Missing);

 

And here's what the code looks like if you are using the extension method shown above:

 

 

app.Workbooks.Open("c:\\temp\\file.xlsx");

That's about it - you should be able to do this for every method that takes optional parameters.

You can download the OverGen tool to enable you to create such extension method for every PIA you use.

You can also download ready-made extension methods for Excel and Word.