Visual Studio 2005 Extensibility and Custom Tools

Cristian Merighi () 0.00

A teasing project since long time, recently the occasion to dedeicate myself to it and now the huge satisfaction for the success: creating a custom tool "Code Generator" for Visual Studio 2005.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

Wow! ...errr ...It's not about Vista, it's about Visual Studio Extensibility!
In my constant seek for code reusability and easeability (do you like this neologism of mine?), I've been figuring out how to generate code using a simple config file (an xml which is subject to a precise schema). Just to be clearer, the way it happens when you use Resource files (.resx), or Typed Datasets (.xsd) in your projects.

The goal to reach was challenging - I'm a total newbie in Visual Studio Extensibility and the documentation about is pretty poor actually - so I tried...

Preannouncing my big success :), I'm not going to put code for this article but something more important: a to-do list in order to get your codegenerator registered and useable in Visual Studio 2005.

  1. First of All. Create a Class Library project and add the following references:
    • EnvDTE;
    • EnvDTE80
    • Microsoft.VisualStudio.OLE.Interop;
    • Microsoft.VisualStudio.Shell;
    • Microsoft.VisualStudio.Shell.Design;
    • Microsoft.VisualStudio.Shell.Interop;
    • Microsoft.VisualStudio.Shell.Interop.8.0;
    • VSLangProj;
    • VSLangProj2;
    • VSLangProj80.
  2. The fooCodeGenerator class you're going to develop must implement the following interfaces:
    • Microsoft.VisualStudio.Shell.Interop.IVsSingleFileGenerator
    • Microsoft.VisualStudio.OLE.Interop.IObjectWithSite
    This could be a very resource spending task to accomplish but, fortunately, you can lean on Microsoft's Visual Studio SDK which provides the nugget sample project "SingleFileGenerator. You can use the base implementation you find there.
  3. Once you have successfully built your project, do not forget to assign a strong name to the assembly, because it has to be put into the global assembly cache (GAC).
    So use the sn.exe tool and sign it!
  4. To register your assembly into the GAC, you can use the gacutil.exe via commandline:
    c:\> gacutil /i e:\mypath_to_the_assembly\myassembly.dll
  5. Now you have to register the same assembly in order to have COM visibility.
    I forgot to mention that you must mark the main class in your project with the System.Runtime.InteropServices.ComVisibleAttribute set to true.
    You have the regasm.exe tool for that:
    c:\> regasm e:\mypath_to_the_assembly\myassembly.dll
  6. Last, but not least, the registry merging of VisualStudio keys related to the Generators section:
    You must mark your class with a randomly generated System.Runtime.InteropServices.GuidAttribute. Then use the same Guid, the chosen Custom Tool name, and the fully qualified assembly name of your GAC registered assembly to write the following .reg file:
    "Assembly"="Pacem.VisualStudio, Version=, Culture=neutral, PublicKeyToken=659a547d5fd6a604"
    @="Pacem.Data.Adapters Class Generator"
    @="Pacem.Data.Adapters Class Generator"
    @="Pacem.Data.Adapters Class Generator"

...Et voila!
Look at the followin screenshots:

adaptx codegenerator

Hope it helped.

Take care. Bye.




Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated


Where am I?


Cristian Merighi facebook twitter google+ youtube

I'm now reading