
184
|
Chapter 6, Speed Hacks
#47 Build a Custom Tool
HACK
format and then place it in the Guid attribute on your class. Here is what
your code should now look like:
using System;
using System.Runtime.InteropServices;
using CustomToolGenerator;
namespace CustomToolProject
{
[Guid("8696CA73-4FD7-4e5f-B267-48C9F3CB8F07")]
public class CustomGenerator : BaseCodeGeneratorWithSite
{
protected override byte[ ] GenerateCode(string file,
string contents)
{
//Code from the preceding example goes here
}
}
}
Add the Tool to Visual Studio
After you have written the code to generate the code for your custom tool,
the next step is to register your custom tool with Visual Studio. The first
thing to do is register your class as a COM component using the regasm
tool. Here are the steps to register your class:
1. Run the Visual Studio Command Prompt (Start
➝ Visual Studio 2003 ➝
Visual Studio .NET Tools ➝ Visual Studio .NET 2003 Command
Prompt).
2. Navigate to the directory where your .dll file is located (most likely Your
Project/bin/debug).
3. Type the following command:
regasm /codebase your_assembly.dll.
4. Next, you will get a warning if your tool is not signed with a strong
name. You can ignore this for now (although signing your tool before
distributing it is recommended).
Now that your tool is registered, you need to tell Visual Studio where and
what the tool is. This is done through the registry. You will need to