繁体   English   中英

如何获取t4文件在Visual Studio中构建?

[英]How to get t4 files to build in visual studio?

当我构建我的C#解决方案时,.tt文件将不会创建.cs文件输出。 但是,如果我在解决方案资源管理器中一次右键单击.tt文件,然后选择“运行自定义工具”,则会生成.cs,因此构建工具设置正确。 我如何才能获得整体解决方案的构建以强制自定义工具在.tt文件上运行?

保罗,您还可以在构建时使用TextTransform.exeElton Stoneman的MSBuild任务生成代码。 只需记住,当T4在Visual Studio与命令行主机中运行时,内置指令(如汇编包含)的行为是不同的。

回答我自己的问题,应该按照以下讨论在设计时生成它们:

https://web.archive.org/web/20081227142303/http://www.olegsych.com/2008/02/t4-template-directive/

在Visual Studio 2013中,通过将以下行添加到.csproj文件中,我能够获取.tt文件来重新生成其目标:

<PropertyGroup>
  <!-- Get the Visual Studio version – defaults to 10: -->
  <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
  <!-- Keep the next element all on one line: -->
  <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<!-- To enable build tasks on your development computer, install Modeling SDK for Visual Studio. https://www.microsoft.com/en-us/download/details.aspx?id=40754 -->
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
<!-- Run the Transform task at the start of every build -->
<PropertyGroup>
  <TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<!-- Overwrite files that are read-only, for example because they are not checked out -->
<PropertyGroup>
  <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
</PropertyGroup>
<!-- Transform every template every time -->
<PropertyGroup>
  <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>

但是,要使其正常工作,您需要安装Visual StudioModeling SDK 我在以下页面上找到了所有这些信息,以及对可用选项的更完整描述: 构建过程中的代码生成

在Visual Studio 2017中(可能也是下一版本),应在Pre-build事件中添加此内容:

"$(DevEnvDir)TextTransform.exe" -out "$(ProjectDir)YourTemplate.cs" "$(ProjectDir)YourTemplate.tt"

ps唯一对我有用的解决方案。

pss如果模板不在根项目目录中,请更改其路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM