Build Your Own Custom Tool for Visual Studio Using Ivssinglefilegenerator Interface

This article will guide you through the process of creating a custom tool for Visual Studio by implementing IVsSingleFileGenerator interface.

A custom tool for Visual Studio is a COM component that implement IVsSingleFileGenerator interface, which can transform an input file in the solution into another format.

There are two methods to implement for IVsSingleFileGenerator interface:

  • IVsSingleFileGenerator.DefaultExtension – This method is invoked to determine if the custom tool is applicable to the selected file according to its file extension.
  • IVsSingleFileGenerator.Generate – When the project is built, this method is invoked to convert the file.

IObjectWithSite interface can also be implemented to get context information from sources other than the file itself.

.NET BCL already provided a base class BaseCodeGeneratorWithSite which already implemented IVsSingleFileGenerator  and IObjectWithSite to simplify the implementation.

  • Derive from BaseCodeGeneratorWithSite class

[Guid("Your Guid Here")]

public class MyGenerator : BaseCodeGeneratorWithSite

{

  protected override byte[] GenerateCode(string fileName,

                                        string fileContents)

  {

    …

  • Register the COM object with regasm.
  • Modify system registry to let Visual Studio aware of this external tool. The relevant key is

HKEY_LOCAL_MACHINESOFTWAREMicrosoftVisualStudioVSVersionGeneratorsYourGuidYourClass

Loading