Implementing Extension Methods in VB.NET
Compiling .NET 3.5 Beta 1 Code
If you try to compile the code in Visual Studio 2005, you will get a compiler error for the call String.Speak. You will need to use the .NET 3.5 VBC compiler. You can compile the module easily with a command line statement that invokes the correct compiler and adds library references. Here is the command line I used.
C:WINDOWSMicrosoft.NETFrameworkv3.5.20404vbc module1.vb/out:sp.exe /reference:Microsoft.VisualBasic.dll /reference:"C:Program Files Reference AssembliesMicrosoftFrameworkv3.0System.Speech.dll" /libpath:"C:WINDOWSMicrosoft.NETFrameworkv3.5.20404
Make sure you add a reference to the Microsoft.VisualBasic.dll, the System.Speech.dll, and set the library path to point to the .NET 3.5 framework. The output is sp.exe. You can name it anything you'd like.
Extension methods are already used a lot of places in the upcoming versions of .NET. For instance, they make the yield return pair work with iterators. You can easily create extension methods by defining a method in a module and adding the ExtensionAttribute to functions. Because modules are really classes, there is no problem adding modules anytime you need an extension class.
If you are interested in the plumbing, get a copy of Lutz Roeder's Reflector to look at the MSIL. It is probably reasonable to see some evolution in grow Extension methods work, like class support and perhaps property support. Check out Scott Guthrie's blog to stay up to date on new .NET features.
Finally, before Chairman Bill retires, I'd like to invite him to speak at our user group, glugnet.org, and I'd like to thank the always entertaining Martin Shoemaker for presenting his cool toy DeeJay and Microsoft's improved Speech capabilities for Vista. (Sorry about the clacking keyboard Thursday; I had a deadline.)
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his new book, UML DeMystified, from McGraw-Hill/Osborne. Paul is an architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at firstname.lastname@example.org.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.
Copyright © 2007 by Paul T. Kimmel. All Rights Reserved.
Page 2 of 2