Wednesday, April 29, 2009

Cum Grano Salis

 

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.

Cum Grano Salis

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");

No comments: