T4 Toolbox: Visual Basic as Template Language
As of version 9.7, T4 Toolbox now supports Visual Basic as the template language in addition to C#. You can now create text templates in Visual Basic and perform advanced code generation tasks such as create multiple output files from a single template, automatically add output files to one or more target projects, automatically check out files from source control system if they were modified during regeneration and more.
T4 Toolbox now comes with a set of project item templates for Visual Basic. Here is what you will see when adding a new item to a Visual Basic project.

If you are familiar with these project item templates in C#, you will recognize the code in Visual Basic.
File
<#@ template language="VBv3.5" hostspecific="True" debug="True" #> <#@ output extension="txt" #> <#@ include file="T4Toolbox.tt" #> <# ‘ <copyright file="File1.tt" company="Your Company"> ‘ Copyright © Your Company. All Rights Reserved. ‘ </copyright> #>
Template
<#+ ‘ <copyright file="Template1.tt" company="Your Company"> ‘ Copyright © Your Company. All Rights Reserved. ‘ </copyright> Public Class Template1 Inherits Template Public Overrides Function TransformText() Return Me.GenerationEnvironment.ToString() End Function End Class #>
Generator
<#+ ‘ <copyright file="Generator1.tt" company="Your Company"> ‘ Copyright © Your Company. All Rights Reserved. ‘ </copyright> Public Class Generator1 Inherits Generator Protected Overrides Sub RunCore() End Sub End Class #>
Entire functionality of the core framework of T4 Toolbox is now available to Visual Basic developers. I have updated the T4 tutorial series to provide examples in both C# and Visual Basic.
- Creating your first code generator
- Troubleshooting code generation errors
- Debugging code generation files
- Creating reusable code generation templates
- Creating complex code generators
- Reusing code generators on multiple projects
- Handling errors in code generators
- Unit testing code generators
- Making code generators extensible
Under the hood
In order to make Visual Basic support possible, the entire core framework, including TransformationContext, Template, Generator and other classes, was moved from .tt (Text Template) files to .cs (C#) files and is now compiled in the T4Toolbox.dll assembly. Some of the original extensions, such as initialization of the TransformationContext and TestRunner classes had to be reimplemented using custom directive processors.
Note that the set of templates available in Visual Basic is smaller than for C#. That is because ready-to-use code generators, such as AzMan wrapper, Enum SQL view and LINQ to SQL model are still implemented in C#. There are no plans to convert them to Visual Basic at this time.
Having the entire core framework code compiled as part of a normal C# class library project, we will finally be able to use Sandcastle and generate documentation for T4 Toolbox.
Known issues
Developers working primarily with C#, like myself, may be surprised to find that Visual Basic doesn’t display generated output files by default. You have to select the Show All Files button in toolbar of Solution Explorer in Visual Studio in order to see them.
A bigger issue is using Visual Basic in partial templates that contain code meant to be included and never used separately, such as Template and Generator classes. Without the template directive, current version of the T4 Editor assumes the language to be C# and displays a large number of errors for Visual Basic code. As a workaround, you can temporarily add a template directive to such file. To avoid warnings, it has to be removed once the file is included and compiled as part of another .tt file because T4 does not support multiple template directives.
This workaround is quite painful to use. To make this work right, the T4 editor needs to somehow infer the language from files without the template directive. One of the possible ways to do this is with file extensions. For example, partial C# text templates could use extension .ttcs and partial Visual Basic templates could use .ttvb. What do you think? Is there a better way to solve this?



Hi Oleg,
I have a situation where as VB.NET developers our systems are locked down and can not execute MSI files, without having to fill out paperwork. Can I download a zip file amd manually configure and install the T4 toolbox with VS2008?