Creating Simplified Code Generators in VS.NET 2003
Writing Macros to Generate Code
Because Visual Studio .NET is very complex, it is easy to use VS.NET for long periods of time and overlook the Macros IDE. However, the Macros IDE is a relatively easy tool to use and can be employed to aid in on area of productivity, writing code generators.
The Macros IDE can be started from the VS.NET Tools|Macros|Macros IDE menu, and it opens another IDE very similar to VS.NET. The Macro language in .NET is VB.NET (bonus for us) and permits one to tap into the Automation Model for VS.NET and use all of the resources of .NET. As a result, with a little practice one can interact with the most intimate and powerful aspects of VS.NET. Adding new features, automating existing features, and writing powerful yet easy-to-use and implement code generators is a first good step towards hyper-productivity.
Running Macros IDE
Figure 1: The Macros IDE in VS.NET 2003 with a module I created.
To create the new module click on the MyMacros project in the Project Explorer (see the left side of figure 1). The project is a special file with a .vsmacros extension. For example, you can find and share the MyMacros.vsmacros project by searching your hard drive with Windows Explorer. If you want to add the module MyMacros.vb then click the MyMacros project and select Add|Add Module from the context menu. After this step you are writing VB.NET code. The real power comes from the Automation Model for VS.NET exposed as part of .NET in the EnvDTE namespace. As an introduction, we will create our first simple macro.
Creating a Macro
Let's create the canonical Hello World example macro. Implement Hello World example by modifying the macro module as shown in listing 1.
Listing 1: Implementing your first macro.
Imports EnvDTE Imports System.Diagnostics Public Module MyMacros Public Sub HelloWorld() MsgBox("Hello, World!") End Sub End Module
By comparing figure 1 and the code in listing 1 you can see that only the public subroutine HelloWorld was added. Subroutines make up the entry point for macros, but you can write any code you want to write inside of the macro modules or class files.
If you want to run the HelloWorld macro then place the cursor anywhere on the HelloWorld subroutine and select Start from the Macros IDE Debug menu. You may also run the macro from VS.NET by opening the Command Windows from the View|Other Windows menu and typing the full path name to the macro you want to run (see figure 2).
Figure 2: You can run macros from the VS.NET Command Window as shown.
The first half of this two-part article introduced you to the Macros IDE and Macros in VS.NET. By itself a macro might be little more than a novelty. Like every other aspect of .NET it is the framework that the Macro language sits on top of and the Automation Model for VS.NET that makes macros an exceptional tool for .NET.
In the second half of this article I will demonstrate how to employ .NET itself in writing powerful code generators that will help you leverage your time and productivity.
About the Author
Paul Kimmel is a freelance writer for Developer.com and CodeGuru.com. Look for his upcoming book, Visual Basic .NET Power Coding, from Addison-Wesley. Paul Kimmel is available to help design and build your .NET solutions and can be contacted at email@example.com.
# # #
Page 2 of 2