问题
I recently set out on an expedition to unit-test a rather complex T4 class. I've arrived at a major breakthough, but I'm afraid the observed behavior may only be coincidental(ie, 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 such from the T4 template AND compile the code file as normal. However, is prefixing a comment before the <#+
and #>
tags at all defined behavior? Do I risk this breaking in the future?
Also, I've tested this on both MonoDevelop and Visual Studio 2012. It appears to work for both. Visual Studio 2008 compatibility isn't important for me though.
回答1:
I can't say we won't 'ever' break things in future, but I find it hard to imagine what we'd do that would break this. Comments inside the feature block will always be respected and comments before it need to be respected to allow generation of comments. I think you are safe.
You'd have to use fully-qualified names in the generator.tt.cs file I think, as there'd be nowhere to put using statements or namespaces.
How does this compare for you to precompiling the helper library for your tests via some empty stub template and then testing the precompiled version?
来源:https://stackoverflow.com/questions/13171344/is-this-hack-a-defined-behavior-for-t4