【发布时间】:2021-07-18 02:06:20
【问题描述】:
我有一个 T4 模板。
<#@ template language="C#" #>
<#@ import namespace="System.Collections.Generic"#>
<#@parameter name="Stats" type="System.Collections.Generic.List<SiteStat>" #>
<#@parameter name="Cnt" type="System.Int32" #>
This is a test. Why is this not working <#= Cnt #>
<table>
<# foreach (SiteStat stat in Stats)
{ #>
<tr><td>Test name <#= stat.node #> </td>
<td>Test value <#= stat.region #> </td> </tr>
<# } #>
</table>
简单的整数很好地传入系统 但更复杂的 List 不是。
它不会在表格标签内生成任何内容。
2021 年 4 月 26 日编辑: 我的 SiteStat 定义如下
namespace ScheduledJobsService
{
[Serializable]
public struct SiteStat
{
public string region { get; set; }
public string district { get; set; }
public string system { get; set; }
public string node { get; set; }
public float a1score { get; set; }
public float a1delta { get; set; }
public float a7score { get; set; }
public float a7delta { get; set; }
public SiteStat(string region, string district, string system, string node, float a1score, float a1delta, float a7score, float a7delta)
{
this.region = region;
this.district = district;
this.system = system;
this.node = node;
this.a1score = a1score;
this.a1delta = a1delta;
this.a7score = a7score;
this.a7delta = a7delta;
}
}
}
我尝试在我的 teplate 中使用 ScheduledJobsService.SiteStat
<#@parameter name="Stats" type="System.Collections.Generic.List<ScheduledJobsService.SiteStat>" #>
但它不会编译声称 SiteStat 在 ScheduledJobsService 命名空间中不存在
【问题讨论】:
-
List<SiteStat>? SiteStat 类型不是此模板中包含的类型。此外,我会使用 XML 或 JSON 传递数据并在模板内序列化,因为传递类型参数的类型既烦人又棘手