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.

Add New Item dialog

If you are familiar with these project item templates in C#, you will recognize the code in Visual Basic.

<#@ 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>

‘ <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

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

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?

Information and Links

Other Posts
Customizing TFS Process Guidance
MVP Award

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

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?

Matt, Although I could provide you with a list of manual setup instructions, you will need permissions to modify Program Files directory and HKEY_LOCAL_MACHINE registry keys, which may also be restricted in your environment. Do you think your network administrator would be open to deploying the T4Toolbox.msi for you?

I have admin rights on the machine but I will go through the interigation process if it is too much trouble. BTW love your blog it has been very helpful.

Updated source code example to be compatible with version 9.10 of the T4 Toolbox.

I’ve set template language=”VBv3.5″, but I can’t use type inference. For example, I have to set stuff like
For Each entityType As Type In typeList
instead of
For Each entityType In typeList
What am I doing wrong? Does it work in C#?

P.S. I just noticed it works in class feature blocks. What’s the difference?



When you pointed this out, I went back and tested some scenarios. What may be happening here is that T4 invokes Visual Basic compiler without specifying the /optioninfer+ command line option. Although the documentation on MSDN says it is turned on by default, the actual behavior appears to be different.

You may have to use explicit typing as a workaround. Perhaps this will be fixed in a future version of T4.

P.S. I haven’t noticed a difference between class feature and statement blocks regarding this behavior.