我正在编写一个T4脚本,它反映了某些类,并提供基于它们的代码生成.问题是我的脚本错误,说我当前项目中的类无法访问.
脚本本身与我尝试引用的类位于同一个程序集中.我已经尝试引用命名空间,文件并添加对当前程序集(项目本身)的引用 - 一切都无济于事.
我错过了什么?
是否可以将T4模板输出与现有文件合并?
例如,如果T4模板生成本地化资源XML文件,是否可以将它们合并到一些现有资源文件中?
我是一个T4新手试图使用T4工具箱根据这个答案生成F#代码,但似乎类功能块不能与语句块混合使用.这是我的代码:
<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#
FSharpTemplate template = new FSharpTemplate();
template.Output.Project = @"..\Library1\Library1.fsproj";
template.Output.File = "Module2.fs";
template.Render();
#>
<#+
class FSharpTemplate: Template
{
public override string TransformText()
{
#>
module Module2
<# for (int i = 0; i < 10; i++) { #>
<#= i #>
<# } #>
<#+
return this.GenerationEnvironment.ToString();
}
}
#>
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
在模板中的第一个类功能之后,语句无法显示.在第一个类功能块之后,只允许使用样板,表达式和其他类功能.
那么......我怎样才能重写模板来实现这个目标呢?
我正在尝试在我的项目中使用这样简单的方案:
Item.cs - >这包含具有简单实体属性的c#partial类.它还设置了属性"Custom Tool:T4ScriptFileGenerator",因此它将"分组"文件Item.tt附加到它.至极的道路上产生Item.generated.cs,分组以Item.tt.因此,它会在解决方案资源管理器中生成一个分组树:
Item.cs
?Item.tt
?Item.generated.cs
Run Code Online (Sandbox Code Playgroud)
您可以在下面看到它在.csproj文件中的外观.
整个项目已连接到TFS 2010工作区.
问题是:
当我编辑Item.cs时,t4引擎重新创建Item.generated.cs文件,但不会从tfs中检出它.它会自动检查出唯一的"分组"的文件前两个层次- Item.cs及其相关Item.tt.该Item.generated.cs被更新,但简单的自动覆盖,没有结账.
如果我编辑Item.tt文件或只是按下"运行自定义工具" - 再次签出两个文件:Item.tt及其相关的Item.generated.cs 这很奇怪,因为迫使我们手动"运行自定义工具"编辑实体类文件后的每个*.tt文件,以确保TFS没有遗漏任何内容.
问:在编辑主Item.cs时,有没有办法强制它检查DependentUpon文件的整个树?
以下是它在.csproj文件源中的外观:
<Compile Include="Entities\Item.cs">
<Generator>T4ScriptFileGenerator</Generator>
<LastGenOutput>Item.tt</LastGenOutput>
</Compile>
<None Include="Entities\Item.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Item.cs</DependentUpon>
<LastGenOutput>Item.codegen.cs</LastGenOutput>
</None>
<Compile Include="Entities\Item.codegen.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Item.tt</DependentUpon>
</Compile>
Run Code Online (Sandbox Code Playgroud) 如何在模板中使用生成的文件名?我想要这样的事情:
// This file: <#= OutputFileName #> was autogenerated
Run Code Online (Sandbox Code Playgroud)
我怎么能在T4或T4Toolbox中做到这一点?
我想VolatileAssembly
从T4工具箱中使用但不想要求所有开发人员安装T4工具箱.
是否可以在源代码管理中包含带有项目的DLL并通过路径引用它?