Is this hack a defined behavior for T4

落花浮王杯 提交于 2019-12-12 17:16:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!