February 20, 2019
Hot Topics:

Write Macro Code Generators with VS 2005

  • July 27, 2005
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Test the Macro

The easiest way to test a macro is running it in the Macros IDE. An easy way to test the code in context is to switch to the VS 2005 IDE, open the Command Window (View|Other Windows|Command Window), and type the entire macro subroutine name, including namespaces, at the Command Window prompt. Based on my implementation, you would select any field and enter the following in the IDE's Command window:

The Intellisense feature in the IDE will help you find the path to the macro if your path differs from mine.

Add the Macro to the Menubar

Having tested the refactoring Encapsulate Field, you can add it to the Visual Studio menubar or toolbar. To add access to the macro on the Tools menu, follow these steps:
  1. In Visual Studio 2005, select Tools|Customize.
  2. Pick the Command tab.
  3. Select Macros from the Categories list and the macro you just created from the Commands list.
  4. Drag and drop the macro on to the Tools menu.
  5. Right-click on the command now residing on the Tools menu, use the context menu to change the menu name—to something like "Encapsulate Field", and add an icon if you'd like (see Figure 1).
  6. Close the Customize dialog.

Figure 1: Drag the Macro to the Menubar Where You'd Like It to Be Accessible

That's all there is to it. Your custom refactoring code generator is now an integrated part of your copy of the IDE. If you want to amaze your friends, locate the file mymacros.vsmacros (or export the module containing the refactoring) and email it to them, along with the instructions for integrating the macro into the IDE. When they tell you how clever you are and you bask in the glory for a while, just tell them where you got the idea. ; )

Ready for a Leap Forward

This article spotlighted an underutilized but powerful aspect of Visual Studio: macros and code generators. I really hope that we are on the cusp of more powerful and more interesting software systems. I still have at least 25 productive years left in me and hope to be around to see software that becomes truly generative and more interesting as a result.


Without question, the accidental father of modern software design is Christopher Alexander. Oddly enough, Mr. Alexander writes about conventional architecture—buildings. The reason he is the father of modern design is because he writes about architecture from the basis of architectural patterns, and his ideas were openly adopted for modern software design. (Just look in the back of any book on design patterns and the like and you will see a bibliographical reference to A Pattern Language: Towns, Buildings, Construction. All of the comparisons to home building on software books is no accident.) Although I haven't met Mr. Alexander (but do have at least one copy of his book), it appears that he is with us and still writing. Presently, he is writing about the generative nature of things. I am curious to see what impact this will have on our industry.

About the Author

Paul Kimmel has written several books on object-oriented programming and .NET. Check out his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005). Paul is also the founder and chief architect for Software Conceptions, Inc., founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at pkimmel@softconcepts.com. If you are interested in joining, sponsoring a meeting, or posting a job then checkout www.glugnet.org, the Web page of the Greater Lansing area Users Group for .NET.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date