T4生成文件中的csproj文件中没有DependentUpon属性 [英] t4 generate file with no DependentUpon attribute in csproj file
问题描述
我们使用T4模板,在我们的项目管理配置。而web.config文件是通过web.tt文件生成。代的csproj文件后,我有以下几点:
We are using t4 templates for managing configurations in our project. And web.config file is generated via web.tt file. After generation in csproj file I have following:
<Content Include="Web.config">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Web.tt</DependentUpon>
</Content>
是否有可能以某种方式配置T4模板来生成独立的web.config文件,不属于web.tt?
Is it possible to configure t4 template somehow to generate independent web.config file, not under web.tt?
推荐答案
当然可以。首先定义一个保存在T4程序包含文件。
Yes you can. First define a save routine in a T4 include file.
SaveOutput.tt
<#@ template language="C#" hostspecific="true" #>
<#@ import namespace="System.IO" #>
<#+
void SaveOutput(string fileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, fileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>
现在每次调用SaveOutput使用的文件名时将会写入当前缓冲区到外部文件。例如,
Now every time you call SaveOutput with a file name it will write the current buffer to that external file. For example.
Web.tt
<#@ include file="SaveOutput.tt" #>
<#
GenerateConfig();
SaveOutput("..\..\Web.Config");
#>
如果你想生成多个文件:
If you want to generate multiple files:
<#@ include file="SaveOutput.tt" #>
<#
GenerateFile1();
SaveOutput("..\..\File1.txt");
GenerateFile2();
SaveOutput("..\..\File2.txt");
#>
这篇关于T4生成文件中的csproj文件中没有DependentUpon属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!