Is this a hacking specific behavior for T4

I recently went on an expedition for unit testing a fairly complex T4 class. I came to a major breakthrough, but I am afraid that the observed behavior can only be random (i.e. it may break in future versions of Visual Studio)

I basically have something like this:

MainTemplate.tt:

<#@ include file="generator.tt.cs" #> 

And then in generator.tt.cs I have

 //<#+ class code { .... } //#> 

The observed behavior of this is that I can both use the declared classes, and from the T4 template AND compile the code file as usual. However, the comment prefix before the <#+ and #> tags for any specific behavior? Can I risk it in the future?

In addition, I tested this on both MonoDevelop and Visual Studio 2012. It works for both. Visual Studio 2008 compatibility is not important to me.

+2
undefined-behavior visual-studio t4 forward-compatibility
source share
1 answer

I canโ€™t say that we wonโ€™t โ€œeverโ€ break things in the future, but itโ€™s hard for me to imagine what we will do to break this. Comments inside the function block will always be respected and comments before you need to follow them in order to generate comments. I think you are safe.

You will need to use fully qualified names in the generator.tt.cs file, I think, since there would be nowhere to use operators or namespaces.

How does this compare with you to pre-compile a helper library for your tests through some empty stub template, and then to test a pre-compiled version?

+3
source share

All Articles