我在单独的文件中拆分T4代码以实现模块化和重用,但我发现每个文件在输出中都是空行.例如:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".ttinclude" #>
<#@ Import Namespace="System.Collections.Generic" #>
<#@ Include file="Includes.tt" #>
namespace <#= NameSpace #>
{
Run Code Online (Sandbox Code Playgroud)
如果Includes.tt列出了3个其他*.tt文件,我在命名空间前得到3个空行.当我添加代码并将其拆分为单独的*.tt文件时,这个空白空间不断增长.事实上,我将所有包含文件打包到一个Includes.tt中,希望这只花费我一行空.它没有.我仍然在Includes.tt中列出的每个文件中得到一个空行.有没有办法避免这种情况?
编辑:假设我不是一个愚蠢的错误(我真诚地希望我是),问题并不像第一眼看上去那么微不足道:
a)通过包含的T4文件重用和模块化与T4本身一样古老,并在最新的MSDN杂志文章中提到:"在T4代码生成解决方案中管理复杂性".
b)如果代码是自动生成的,那并不意味着它的格式错误或清晰度差.
c)使用当前的解决方案,在我的情况下,对于每个生成的.cs文件,读者必须滚动一个空页面,直到她开始看到一些生成的文本.所有这些都是因为我在多个包含的.tt文件之间分割了我的代码.这似乎不对.
Ton*_*ony 15
好吧,解决方案结果是微不足道的,如果有些意外:只需将include指令放在彼此旁边,而不是在另一个下面:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".ttinclude" #>
<#@ Import Namespace="System.Collections.Generic" #>
<#@ Include file="Usings.tt" #> <#@ Include file="PropertyTypeEnum.tt" #> <#@ Include....
Run Code Online (Sandbox Code Playgroud)
B3r*_*ret 13
添加到Tony的答案:你可以通过在T4括号内添加换行符来避免很长的行,如下所示:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".ttinclude" #>
<#@ Import Namespace="System.Collections.Generic" #>
<#@ Include file="Usings.tt"
#><#@ Include file="PropertyTypeEnum.tt"
#><#@ Include....
#><#@ Include....
#><#@ Include....
#><#@ some other stuff
Run Code Online (Sandbox Code Playgroud)
Car*_*lsh 13
我有一个更基本的问题,每个<#@标题行之前<?xml在输出中导致自己的空行,这导致错误:
error : Unexpected XML declaration.
The XML declaration must be the first node in the document,
and no white space characters are allowed to appear before it.
Line 7, position 3.
Run Code Online (Sandbox Code Playgroud)
挖了一段时间后,我发现.tt文件有Unix EOL.
当我切换到Windows EOL时,转换删除了空白行.
El *_*rko 12
这在VS 2013中也适用于我:
<#@ include file="Other.tt" #><##>
Run Code Online (Sandbox Code Playgroud)
所以
<#@ include file="One.tt" #><##>
<#@ include file="Two.tt" #><##>
...
Run Code Online (Sandbox Code Playgroud)
这<##>只是一个空的控制块.<# /* any code here */ #>同样有效.