【问题标题】:How to output <Type> inside T4 template如何在 T4 模板中输出 <Type>
【发布时间】:2016-02-02 17:07:44
【问题描述】:

我正在尝试实现T4 template 来生成一些冗余的csharp 代码。我的模板获取传入的对象类型如下GenericTextFormatter&lt;&lt;#=type&gt;&gt;,其中类型为typeof(objectA) 等。所以我希望生成GenericTextFormatter&lt;ObjectA&gt; 的输出,但是模板在显示外部尖括号时遇到问题,相反,我看不到此输出部分。

【问题讨论】:

  • t4 是 text 模板,所以你应该传递给输出不是type对象,而是输入名称,或者输入全名
  • 上面用导致我的问题的代码描述了问题。我相信熟悉 T4 模板的人一定看过这个。

标签: c# code-generation t4


【解决方案1】:

你忘记了结束的#。试试GenericTextFormatter&lt;&lt;#=type #&gt;&gt;。

作为参考,下面的t4代码输出List&lt;System.String&gt;:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>

<#
    var type = typeof(string);
#>

List<<#= type #>>

【讨论】:

  • 就是这样,完全失去了注意力,谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
相关资源
最近更新 更多