Use T4 Templates For XSLT Transformation

Suppose you have XSLT style sheets that produce some code from XML files. Now suppose you want to integrate these style sheets into your Visual Studio solution to generate code artefacts. You could call command line tools from the pre-build event to process the XSLT files. Alternatively, you could somehow bring the XSLT markup into a T4 template and let the regular Visual Studio integration initiate execution of the template. That is actually not that difficult, but there is one major obstacle. Continue reading

Resolve Visual Studio Macros In VSIX Packages Or T4 Templates

We all love Visual Studio Macros, right? Pre- and Post-Build actions or MSBuild scripts would get very nasty if there were no macros like “ProjectDir”, “ConfigurationName” or “OutDir”. If these macros are so useful, then why not make use of them in your own VSIX packages or T4 templates? If your answer is “because I got lost in the maze of Visual Studio’s object model and it’s too dark to see” then fear not! You will find your way out very soon. Continue reading

Inheriting a T4 Template and Calling EnvDTE.DTE: The System Cannot Find The File Specified

Some strange behavior occurred today when I tried to encapsulate some functionality of a T4 template into a base class. The base class should load the Visual Studio automation object (DTE), but that led to an error stating that “the system cannot find the file specified”. “the file” was the assembly that contains my base class – i.e. the assembly that was currently running. So what’s going on here? Continue reading

Get The ProjectItem of a T4 Template From Inside the T4 Template

So you got a T4 template and need access to the ProjectItem of that T4 template or to the parent Project of the T4 template? Here’s how:

1) Set the template’s hostspecific attribute to true and reference the Visual Studio Automation assemblies.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="envdte" #>
<#@ assembly name="envdte80" #>
<#@ import namespace="EnvDTE" #>

2) Get the Project or ProjectItem using Visual Studio Automation.

// By setting the template's 'hostspecific' property to true, 
// we get access to the text templating engine host 
// which implements IServiceProvider.
var serviceProvider = (IServiceProvider)this.Host;

// Get the automation root object
var dte = (EnvDTE.DTE)serviceProvider.GetService(typeof(EnvDTE.DTE));

// Get the project item
var projectItem = dte.Solution.FindProjectItem(this.Host.TemplateFile);

// Get the project
var project = projectItem.ContainingProject;

In-Memory Run-Time Code Generation With .NET – Part 4

This article is split into 5 posts.

  1. Introduction
  2. Templating Engines
  3. Code Generation From Source
  4. Code Generation From Model
  5. Loading

Continue reading

In-Memory Run-Time Code Generation With .NET – Part 3

This article is split into 5 posts.

  1. Introduction
  2. Templating Engines
  3. Code Generation From Source
  4. Code Generation From Model
  5. Loading

Continue reading

In-Memory Run-Time Code Generation With .NET – Part 2

This article is split into 5 posts.

  1. Introduction
  2. Templating Engines
  3. Code Generation From Source
  4. Code Generation From Model
  5. Loading

Continue reading