Directive processors work by adding code to the generated transformation class. The generated transformation class is then compiled and executed to produce the generated text output. You call directives from a text template, and the engine processes all the directive calls as part of creating the generated transformation class. After you call a directive, the rest of the code you write in your text template can rely 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 to provide custom functionality to 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#
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.
A new class library project is created, and 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, after the existing using statements, add the following code:
|
using Microsoft.VisualStudio.TextTemplating;
|
-
Locate the following code:
-
Highlight Class1 and type a new name. This will be the name of your directive processor class. For example, type CustomDirectiveProcessor.
-
Put your cursor after the new class name and type the following code:
-
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, and you must 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.
A new class library project is created, and 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.TextTemplating
|
-
Locate the following code:
|
Public Class Class1
End Class
|
-
Highlight Class1 and type a new name. This will be the name of your directive processor class. For example, type CustomDirectiveProcessor.
-
Put your cursor after the new class name and press enter to go to the next line. Type the following code, but do not press enter yet:
|
Inherits DirectiveProcessor
|
-
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, and you must 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