难道这劈为T4一个定义的行为定义、行为

2023-09-05 04:19:12 作者:酒醒天寒

我最近成立了远征到单元测试一个相当复杂的T4级。我已经到了一个重要的找准突破口,但恐怕观察到的行为可能只是巧合(即,在Visual Studio的未来版本可能会破坏)

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)

我基本上是这样的:

MainTemplate.tt:

MainTemplate.tt:

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

然后在generator.tt.cs我有

And then in generator.tt.cs I have

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

这样做的观察到的行为是,我既可以用声明的类和这样的T4模板和编译code文件是正常的。然而,为prefixing之前评论的&LT;#+ #&GT; 标签在所有定义的行为?难道我这个冒险破的未来?

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?

另外,我在两个MonoDevelop的和Visual Studio 2012年它似乎可同时用于测试这一点。 Visual Studio 2008的兼容性不是对我很重要,但。

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.

推荐答案

我不能说,我们不会'永远'在未来的破事,但我觉得很难想象我们会做这将打破这个。该功能块内部的意见将永远得到尊重,才需要意见得到尊重,让一代人的意见。我认为你是安全的。

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.

您不得不在generator.tt.cs文件,我想使用完全限定的名称,因为会是无处using语句或命名空间来放。

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.

这怎么比较,为您$ P $通过一些空存根模板pcompiling为你的测试辅助库,然后测试precompiled版本?

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?