【发布时间】:2020-08-06 23:19:54
【问题描述】:
我做了什么
我尝试将我的 T4 模板编译成 c# 文件。
我从 Microsoft: Invoke text transformation in the build process
尝试过的内容通过添加我的 .csproj 文件:
<Import Project="TextTemplating\Microsoft.TextTemplating.targets" />
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
<ItemGroup>
<T4ParameterValues Include="ProjectDir">
<Value>$(ProjectDir)</Value>
<Visible>false</Visible>
</T4ParameterValues>
</ItemGroup>
TextTemplating 是一个目录,其中包含来自我的编辑器的 TextTemplating 文件,位于:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\msbuild\Microsoft\VisualStudio\v16.0\TextTemplating
模板
基础模板(命名为 ModelTemplate.tt):
<#@ template language="C#" #>
<#@ parameter name="ClassName" type="System.String"#>
<#@ parameter name="Namespace" type="System.String"#>
namespace <#= Namespace #>
{
public class <#= ClassName #>
{
}
}
最后是测试ModelTemplate.tt的模板(命名为ModelTemplateTest.tt):
<#@ template debug="false" language="C#" #>
<#@ output extension=".cs" #>
<#
_ClassNameField = "Model";
_NamespaceField = "MyNamespace";
#>
<#@ include file="$(ProjectDir)\Templates\ModelTemplate.tt"#>
构建的输出
A custom tool 'TextTemplatingFilePreprocessor' is associated with file 'Templates\ModelTemplate.tt', but the output of the custom tool was not found in the project. You may try re-running the custom tool by right-clicking on the file in the Solution Explorer and choosing Run Custom Tool.
但是ModelTemplateTest.tt被编译成:
namespace MyNamespace
{
public class Model
{
}
}
如何在我的构建中调用TextTemplatingFilePreprocessor?
【问题讨论】:
标签: c# .net visual-studio templates t4