我想在运行时基于T4模板构建一些输出。我有这个模板,看起来它工作得很好,但是当我尝试通过msbuild构建它时,出现了问题。此时,模板获取的是顶级命名空间,而不是Visual Studio 2019给出的特定于目录的命名空间。这意味着如果我在VS中构建代码,得到的是一种结果,但如果我在我们的CI工具中构建代码,则得到完全不同的东西。显然,这是行不通的。
我有
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v16.0\TextTemplating\Microsoft.TextTemplating.targets" />
我在我的csproj文件中添加了某些内容,但当我运行了msbuild后,得到的输出结果如下:
ExecuteTransformations:
Performing full T4 transformation
Performing full T4 preprocessing
Preprocessing template Templates\MyMaster.tt...
Preprocessing template Templates\Pdf\MyType.tt...
所有生成的 cs 文件都在 Root
命名空间中。我期望它们与 Visual Studio 中的相同,即为 Root.Templates
和 Root.Templates.Pdf
。
这些文件的 csproj 如下所示:
<Content Include="Templates\MyMaster.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>MyMaster.cs</LastGenOutput>
</Content>
任何一种能够使预构建的内容具备正确命名空间的方法都是极好的。我宁愿不采用重新变换已生成的CS文件的预构建脚本。
namespace Root.Templates { ... }
添加到您的 tt 文件中呢? - Klaus Gütter