T4 模板在构建期间未转换

loi*_*pez 5 .net c# t4 templates visual-studio

我做了什么

我尝试将 T4 模板编译为 ac# 文件。

我从微软尝试过:在构建过程中调用文本转换

通过添加我的 .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>
Run Code Online (Sandbox Code Playgroud)

TextTemplating是包含我的编辑器中的 TextTemplated 文件的目录,位于:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\msbuild\Microsoft\VisualStudio\v16.0\TextTemplated

模板

基本模板(名为 ModelTemplate.tt):

<#@ template language="C#" #>
<#@ parameter name="ClassName" type="System.String"#>
<#@ parameter name="Namespace" type="System.String"#>

namespace <#= Namespace #> 
{
    public class <#= ClassName #>
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

最后是用于测试ModelTemplate.tt 的模板(名为 ModelTemplateTest.tt):

<#@ template debug="false" language="C#" #>
<#@ output extension=".cs" #>
<#
    _ClassNameField = "Model";
    _NamespaceField = "MyNamespace";
#>
<#@ include file="$(ProjectDir)\Templates\ModelTemplate.tt"#>
Run Code Online (Sandbox Code Playgroud)

构建的输出

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.       
Run Code Online (Sandbox Code Playgroud)

但是ModelTemplateTest.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.       
Run Code Online (Sandbox Code Playgroud)

我如何调用TextTemplatingFilePreprocessor我的构建?

loi*_*pez 3

解决方案(使用 Visual Studio Enterprise 2019)是按如下方式导入文件$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets.csproj

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
  </PropertyGroup>
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
</Project>
Run Code Online (Sandbox Code Playgroud)

相关 StackOverflow 线程:构建时 T4 转换的乘积仅在下一次构建中使用

  • 您可以使用 `&lt;Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplated\Microsoft.TextTemplated.targets" /&gt;` (4认同)