Directive processors add code to the generated transformation class. You can then compile and execute the generated transformation class to produce the generated text output. You call directives from a text template. The engine processes all the directive calls when it creates the generated transformation class. After you call a directive, the rest of the code you write in your text template relies on the functionality provided by the directive. For more information, see Architecture of the Text Template Transformation Process.
You can write your own custom directive processors. This enables you to customize your text templates. To create a custom directive processor, you create a class that inherits from either DirectiveProcessor or RequiresProvidesDirectiveProcessor. For more information, see Walkthrough: Creating a Custom Directive Processor.
This topic contains steps to complete the following tasks:
Creating a Custom Directive Processor in C#
Creating a Custom Directive Processor in Visual Basic
Creating a Custom Directive Processor in C#
To create a custom directive processor in C#
In Visual Studio, on the File menu, point to New, and then click Project.
The New Project dialog box appears.
Under Projects, select the Visual C# node.
Under Visual Studio installed templates, select Class Library.
In the Name box, type a name.
This will be the name of your directive processor namespace. For example, type CustomDP.
Check the Create directory for solution check box.
Click OK.
The system creates a new class library project, which opens in Visual Studio.
On the Project menu, click Add Reference, and then click the .NET tab.
In the Component Name column, locate Microsoft.VisualStudio.TextTemplating, and then select it.
Click OK.
In Solution Explorer, double-click Class1 to open it in the editor.
At the top of the code window, after the existing using statements, add the following code:
using Microsoft.VisualStudio.TextTemplating;Locate the following code:
public class Class1 { }Highlight Class1 and type a new name.
This will be the name of your directive processor class. For example, type CustomDirectiveProcessor.
Put your cursor immediately following the new class name and type the following code:
: DirectiveProcessor注意
You can also inherit from the RequiresProvidesDirectiveProcessor class. For more information about the difference between the two types of directive processors, see Creating Custom Text Template Directive Processors.
In the code window, right-click on DirectiveProcessor, and then click ImplementAbstractClass.
Visual Studio will fill in the class with the methods from the DirectiveProcessor class that must be overridden. The methods will be empty. You need to write the code to implement them.
Fill in the code for your custom directive processor.
For more information, see Walkthrough: Creating a Custom Directive Processor. For the documentation of the individual methods, see DirectiveProcessor or RequiresProvidesDirectiveProcessor.
On the File menu, click Save All.
On the Build menu, click Build Solution.
Creating a Custom Directive Processor in Visual Basic
To create a custom directive processor in Visual Basic
In Visual Studio, on the File menu, point to New, and then click Project.
The New Project dialog box appears.
Under Projects, select the Visual Basic node.
Under Visual Studio installed templates, select Class Library.
In the Name box, type a name.
This will be the name of your directive processor namespace. For example, type CustomDP.
Check the Create directory for solution check box.
Click OK.
The system creates a new class library project, which opens in Visual Studio.
On the Project menu, click Add Reference, and then click the .NET tab.
In the Component Name column locate Microsoft.VisualStudio.TextTemplating and select it.
Click OK.
In Solution Explorer, double-click Class1 to open it in the editor.
At the top of the code window, add the following code:
Imports Microsoft.VisualStudio.TextTemplatingLocate the following code:
Public Class Class1 End ClassHighlight Class1 and type a new name.
This will be the name of your directive processor class. For example, type CustomDirectiveProcessor.
Put your cursor immediately following the new class name, and then press enter to go to the next line. Type the following code, but do not press enter yet:
Inherits DirectiveProcessor注意
You can also inherit from the RequiresProvidesDirectiveProcessor class. For more information about the difference between the two types of directive processors, see Creating Custom Text Template Directive Processors.
Press Enter.
When you press enter, Visual Studio will fill in the class with the methods from the DirectiveProcessor class that must be overridden. The methods will be empty. You need to write the code to implement them.
Fill in the code for your custom directive processor.
For more information, see Walkthrough: Creating a Custom Directive Processor. For the documentation of the individual methods, see DirectiveProcessor or RequiresProvidesDirectiveProcessor.
On the File menu, click Save All.
On the Build menu, click Build Solution.
See Also
Concepts
Creating Custom Text Template Directive Processors
Architecture of the Text Template Transformation Process
How to: Set Registry Keys for Custom Directive Processors
Walkthrough: Creating a Custom Directive Processor