如何在T4模板中输出<Type>

How to output <Type> inside T4 template

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

您忘记了结束#。试试 GenericTextFormatter<<#=type #>>.

作为参考,以下t4代码输出List<System.String>

<#@ 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 #>>