Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual Studio SDK
 How to: Create Custom Directive Pro...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Domain-Specific Language Tools 
How to: Create Custom Directive Processors 

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#

  1. In Visual Studio, on the File menu, point to New and then click Project.

    The New Project dialog box appears.

  2. Under Projects, select the Visual C# node.

  3. Under Visual Studio installed templates, select Class Library.

  4. In the Name box, type a name. This will be the name of your directive processor namespace. For example, type CustomDP.

  5. Check the Create directory for solution check box.

  6. Click OK.

    A new class library project is created, and opens in Visual Studio.

  7. On the Project menu, click Add Reference, and then click the .NET tab.

  8. In the Component Name column locate Microsoft.VisualStudio.TextTemplating and select it.

  9. Click OK.

  10. In Solution Explorer, double-click Class1 to open it in the editor.

  11. At the top of the code window, after the existing using statements, add the following code:

    C#
    using Microsoft.VisualStudio.TextTemplating;
    
  12. Locate the following code:

    C#
    public class Class1
    {
    }
    
  13. Highlight Class1 and type a new name. This will be the name of your directive processor class. For example, type CustomDirectiveProcessor.

  14. Put your cursor after the new class name and type the following code:

    C#
     : DirectiveProcessor
    
    NoteNote

    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.

  15. 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.

  16. 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.

  17. On the File menu, click Save All.

  18. On the Build menu, click Build Solution.

Creating a Custom Directive Processor in Visual Basic

To create a custom directive processor in Visual Basic

  1. In Visual Studio, on the File menu, point to New and then click Project.

    The New Project dialog box appears.

  2. Under Projects, select the Visual Basic node.

  3. Under Visual Studio installed templates, select Class Library.

  4. In the Name box, type a name. This will be the name of your directive processor namespace. For example, type CustomDP.

  5. Check the Create directory for solution check box.

  6. Click OK.

    A new class library project is created, and opens in Visual Studio.

  7. On the Project menu, click Add Reference, and then click the .NET tab.

  8. In the Component Name column locate Microsoft.VisualStudio.TextTemplating and select it.

  9. Click OK.

  10. In Solution Explorer, double-click Class1 to open it in the editor.

  11. At the top of the code window, add the following code:

    Visual Basic
    Imports Microsoft.VisualStudio.TextTemplating
    
  12. Locate the following code:

    Visual Basic
    Public Class Class1
    
    End Class
    
  13. Highlight Class1 and type a new name. This will be the name of your directive processor class. For example, type CustomDirectiveProcessor.

  14. 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:

    Visual Basic
    Inherits DirectiveProcessor
    
    NoteNote

    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.

  15. 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.

  16. 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.

  17. On the File menu, click Save All.

  18. On the Build menu, click Build Solution.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker