Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表值传递给t4模板_C#_T4 - Fatal编程技术网

C# 将列表值传递给t4模板

C# 将列表值传递给t4模板,c#,t4,C#,T4,我使用这些代码将参数传递到模板文件 List<string> TopicList = new List<string>(); TopicList.Add("one"); TopicList.Add("two"); TopicList.Add("three"); TopicList.Add("four"); TopicList.Add("five"); PreTextTemplate1 t = new PreTextTemplate1(); t.Session = new

我使用这些代码将参数传递到模板文件

List<string> TopicList = new List<string>();
TopicList.Add("one");
TopicList.Add("two");
TopicList.Add("three");
TopicList.Add("four");
TopicList.Add("five");
PreTextTemplate1 t = new PreTextTemplate1();
t.Session = new Microsoft.VisualStudio.TextTemplating.TextTemplatingSession();
t.Session["TimesToRepeat"] = 5;
foreach (string s in TopicList)
{
    t.Session["Name"] = s;
}
t.Initialize();
string resultText = t.TransformText();
List TopicList=newlist();
主题列表。添加(“一”);
主题列表。添加(“两个”);
主题列表。添加(“三”);
主题列表。添加(“四”);
主题列表。添加(“五”);
PreTextTemplate1 t=新的PreTextTemplate1();
t、 会话=新建Microsoft.VisualStudio.TextTemplating.TextTemplatingSession();
t、 会话[“时间存储重复”]=5;
foreach(主题列表中的字符串s)
{
t、 会话[“名称”]=s;
}
t、 初始化();
字符串resultText=t.TransformText();
但每次,我得到的都是主题列表中的最后一个值(“五”)

<#@ template language="C#" #>
<#@ parameter type="System.Int32" name="TimesToRepeat" #>
<#@ parameter type="System.String" name="Name" #>

<# for (int i = 0; i < TimesToRepeat; i++) { #>
Line <#= Name #>
<# } #>

Actual Output:Line five
              Line five
              Line five
              Line five
              Line five

Expected Output: Line one
                 Line two
                 Line three
                 Line four
                 Line five

线
实际输出:第五行
第五行
第五行
第五行
第五行
预期产出:第一行
第二行
第三行
第四行
第五行
我如何使其能够生成模板中主题列表中的每个值? 像预期的输出


很抱歉,这个问题的英文和格式都很糟糕

我没有使用文本模板,所以让我先说一句,我可能在这里不正确。但就我所看到的,您在模板中定义的名称不正确。请尝试以下操作:

<#@ template language="C#" #>
<#@ parameter type="System.Int32" name="TimesToRepeat" #>
<#@ parameter type="System.Collections.Generic.List<System.String>" name="Names" #>

<# for (int i = 0; i < TimesToRepeat; i++) { #>
Line <#= Names[i] #>
<# } #>

线
您还可以删除TimesToRepeat并改为执行foreach:

<#@ template language="C#" #>
<#@ parameter type="System.Collections.Generic.List<System.String>" name="Names" #>

<# foreach (string name in Names) { #>
Line <#= name #>
<# } #>

线

Ahh。。对就这样!我觉得我快要弄明白了。非常感谢!