gpt4 book ai didi

c# - 包含文件的 T4 "header guard"

转载 作者:行者123 更新时间:2023-11-30 12:11:46 26 4
gpt4 key购买 nike

我正在尝试为代码生成编写一些漂亮且结构化的 t4。我将我的逻辑分解为可重用的函数并将它们放入单独的文件中(就像“普通”代码一样)。问题是我不能包含一个公共(public)文件,因为它会被多次包含。

例如,我有 4 个文件:Core.ttFile1.ttFile2.ttMainTemplate。 ttCore.tt 包含在 File1.ttFile2.tt 中。而 File1.ttFile2.tt 包含在 MainTemplate.tt

当我想从 MainTemplate.tt 生成输出时,出现以下错误:

Error   8   Compiling transformation: The type 'Microsoft.VisualStudio.TextTemplating62CD98C8FF0EB737CAFBD5ED17A158C3.GeneratedTextTransformation' already contains a definition for 'PropertyAttribute'

我猜,原因是 Core.tt 被包含了两次。我的问题有解决方法吗?在 C++ 中,可以添加 header 保护:#ifndef xxx #define xxx #endif

最佳答案

是的,此功能内置于 T4 系统中。

The T4 'include' directive supports a 'once' attribute ,这应确保模板仅包含一次,以防止重复。

例子:

<#@ include file="filePath.tt" once="true" #>

关于c# - 包含文件的 T4 "header guard",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14708745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com