Product of build-time T4 transformation is used only in the next build

雨燕双飞 提交于 2019-11-28 02:40:10

问题


I have a VS project that contains:

  1. a pre-build action running TextTransform on a template.tt to generate generated.cs

  2. generated.cs listed as one of the files to compile (i.e. in the list of project files)

When I build the project, the pre-build action is executed, generated.cs is re-created, but VS compiles the previous version of generated.cs (which I guess it loaded in memory when the build process started).

How to make the build use the new version of generated.cs (i.e. the one generated in the pre-build action)? How to force the build order?

Note that the text transformation input is dynamic and hence cannot be done in design time.


回答1:


I don't think you need a custom pre-build action. Just add the ".tt" file to your project and set its "Custom Tool" property to "TextTemplatingFileGenerator". You might want to make sure that the *.generated.cs files are also added to the project, but I think that VS takes care of that.




回答2:


There's now a solution to this problem! Oleg Sych has a post on his blog detailing how to make transform-at-build-time work.

Here's the source: https://web.archive.org/web/20140116193428/http://www.olegsych.com/2010/04/understanding-t4-msbuild-integration/

Basically, you just include the T4 build targets in your project file and set the TransformOnBuild property to true.

Here's the relevant excerpt:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
  </PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets" />

Note that the Microsoft.TextTemplating.targets file has to be included AFTER the Microsoft.CSharp.targets.



来源:https://stackoverflow.com/questions/1293320/product-of-build-time-t4-transformation-is-used-only-in-the-next-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!