CodeModelMacros Sample: Demonstrates How to Use the Code Model Functions

This sample is a collection of macros that use the Code Model to perform useful tasks. The macros included are:

  • HierarchyGenerator   Generates an HTML file with an inheritance tree for the classes, structs, and interfaces in a project.

    This macro first assembles a collection of the terminal classes, structs, or interfaces (those that don't have any derived types). It does this by verifying that no other code element has the class, struct, or interface in question in its bases collection. Then it generates an HTML report listing the terminal objects with all of its bases. The macro adds this file to the first project in the solution.

  • InterfaceDocGenerator   Generates HTML documentation for the implemented interfaces in a project. It gathers the comments from the class functions that implement the interface methods.

    This macro enumerates the interfaces contained in the CodeModel.Interfaces collection, and then it finds the classes that implement each interface. For each class, it looks up the interface's methods implementation and stores the comments in an HTML file.

  • UserTypeGenerator   Generates a file named usertype.dat containing the names of the classes in your project. If you place this file in the same location where devenv.exe resides (for example, c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE), the source code editor will colorize your class names.

    This macro writes to a file (usertype.dat) the names of the classes contained in the CodeModel.Classes collection.

  • VirtualFunctionFinder   Adds a TODO comment to the virtual functions found in the project.

    The VirtualFunctionFinder finds the virtual functions declared in the project by recursively verifying the functions of each class or struct. Then it adds a comment to each one by changing the CodeFunction.Comment property.

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Installing and Running the Sample

To install the macros

  1. Copy the file CodeModelMacros.vsmacros to your hard drive.

  2. Start Visual Studio.

  3. From the Tools menu, point to Macros and then click Macro Explorer. This will open the Macro Explorer window.

  4. From the Tools menu, point to Macros and then click Load Macro Project. This will open the Add Macro Project dialog box.

  5. Browse to the CodeModelMacros.vsmacros file and click the Open button. This action will load the macros file and the macros will appear in the Macro Explorer.

  6. Expand the CodeModelMacros node. You will see one module for each macro.

To execute HierarchyGenerator

  1. Expand the HierarchyGenerator node in the Macro Explorer.

  2. Double-click the HierarchyGenerator macro. This will execute the macro and add an HTML file to the first project in your solution.

To execute InterfaceDocGenerator

  1. Expand the InterfaceDocGenerator node in the Macro Explorer.

  2. Double-click the InterfaceDocGenerator macro. This will execute the macro and add an HTML file to the first project in your solution.

To execute UserTypeGenerator

  1. Expand the UserTypeGenerator node in the Macro Explorer.

  2. Double-click the UsertypeC macro. This will execute the macro and will create a usertype.dat file in the root directory of your C: drive.

To execute VirtualFunctionFinder

  1. Expand the VirtualFunctionFinder node in the Macro Explorer.

  2. Double-click the MainVirtualFunctionFinder macro. This will execute the macro and will add a TODO comment to the virtual functions declared in your project.

Keywords

This sample demonstrates the following keywords:

CodeClass::Members; CodeElement::CodeTypeFromFullName; CodeElement::Children; CodeElement::FullName; CodeElement::Kind; CodeFunction::CanOverride; CodeFunction::Comment; CodeFunction::InfoLocation; CodeFunction::Name; CodeInterface::Name; CodeModel::CodeElements; CodeModel::Language; CodeNamespace::Members; CodeStruct::Members; CodeType::Bases; Project::CodeModel; Solution::Count; VCCodeBase::FullName; VCCodeBase::Name; VCCodeClass::Functions; VCCodeClass::ImplementedInterfaces; VCCodeClass::IsSelf; VCCodeClass::Name; VCCodeFunction::Attributes; VCCodeFunction::Comment; VCCodeFunction::IsVirtual; VCCodeInterface::Functions; VCCodeModel::Classes; VCCodeModel::Interfaces

See Also

Other Resources

Code Model Samples