Building a Custom Tool Generator For Visual Studio

April 7, 2009 · Posted in .NET, C#, Visual Studio 
Easy AdSense by Unreal

CustomToolForVisualStudio.zip
BaseCodeGeneratorWithSite.dll

Introduction

Sometimes you need to generate your own code for some XML Files in your Visual Studio Project or replace the Resource File Generated Code of the Visual Studio with one of your the reason you may want to do this is that you may want to work with a custom resource provider while the generated code in .NET makes the static properties work only with the resource file.

Using the Code

First to do that you need to inherit from the BaseCodeGeneratorWithSite then generate a GUID for it from Tools -> Create Guid -> Then Select the Registry format and copy the guid to mark your class with this GUID Will let the Visual Studio later on know the class it should call. Then place a GUID and a ComVisible attributes on your class.

    [Guid("A2A52B1B-48A1-45af-A30E-8D86E4DE0D79")]
    [ComVisible(true)]
    public class CustomToolGenerator : BaseCodeGeneratorWithSite

Then Override the GenerateCode Method

   protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
   {
        string code = "Generated Code Should Be Placed Here";
        return System.Text.Encoding.ASCII.GetBytes(code);
   }

If you want the namespace that the File is comming from it’s available in the “FileNameSpace” property of the BaseCodeGeneratorWithSite class.

In the AssemblyInfo.cs Class make the Assembly Com Visible by placing the following code in it

[assembly: ComVisible(true)]

Generate a Strong Name Key for your project using the Visual Studio Command Prompt sn -k CustomToolForVisualStudio.snk and bind your project to it- Right Click on the project then select Properties then Select Signing and choose Sign the assembly property and select the Strong Name Key you have just generated

For Registering the Generator in the Visual Studio you need to place a reference to it in the Registry
Visual Studio 2005
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Generators\
Visual Studio 2008
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\
These are the sub keys for different languages they are named with their GUID you will go inside them
{164B10B9-B200-11D0-8C61-00A0C91E29D5}: Visual Basic
{E6FDF8B0-F3D1-11D4-8576-0002A516ECE8}: J#
{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}: C#

Then Create a key with the name of your class and inside it place the following
(Default): String: CustomToolGenerator (name of the custom tool)
CLSID: String: {A2A52B1B-48A1-45af-A30E-8D86E4DE0D79} (Generated Guid)
GeneratesDesignTimeSource: DWORD: 1

Now Register you Assembly using the resgen command in the Visual Studio Command Prompt

regasm /codebase CustomToolForVisualStudio.dll

Using the Custom Tool in Visual Studio

Right Click on a Resource File or XML File and click properties then place in the CustomTool property the value “CustomToolGenerator

The Custom Tool will now generate the code based on the code given in the “GenerateCode
” Method.

Hope you like it :) Please share your thoughts

Short URL for this post: http://bit.ly/6sf7V

Comments

3 Responses to “Building a Custom Tool Generator For Visual Studio”

  1. [...] Making a custom tool work on the developers machine require several actions like placing keys in the registry and registering your DLL Library using the regasm command. This post will discuss the automation of this procedures using a setup project. for more information on making your own custom tool visit my post Building a Custom Tool Generator For Visual Studio [...]

  2. How I Lost Thirty Pounds in Thirty Days on May 3rd, 2009 7:39 pm

    Hi, interesting post. I have been pondering this issue,so thanks for sharing. I’ll definitely be subscribing to your blog.

  3. ElisabettaDC on January 28th, 2010 3:01 am

    If you go on with your research referring to this good post, I will possibly order dissertation tips from the dissertation service in Internet.

Leave a Reply




Spam Protection by WP-SpamFree

























































download movies

ñêà÷àòü ôèëüìû