我所做的事情
我试图将我的T4模板编译成一个C#文件。
我尝试从Microsoft: 在构建过程中调用文本转换中学到的方法:
通过在我的.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
?
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
。 - StarNamer