<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ assembly name="C:Usersic019672Documentsvisual studio 2015ProjectsPreTemplateLogicPreTemplateLogicbinDebugReadfromCSV.dll" #>
<#@ import namespace="PreTemplateLogic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ parameter type="System.String" name="Pprty" #>
<#@ output extension=".cs" #>
using System;
using System.Collections.Generic;
namespace TemplateLogic
{
public partial class GeneratedClass
{
Dictionary<string, Dictionary<string, string>> dataKey = new Dictionary<string, Dictionary<string, string>>();
public void GenerateParentContainer(string cntrlName)
{
<#
Dictionary<string, Dictionary<string, string>> dataKey = new Dictionary<string, Dictionary<string, string>>();
dataKey = PreTemplateLogic.ReadfromCSV.ReadcsvtoDictionary();
Dictionary<string, string> dc = dataKey["ObjectName"];
string parntKey = "Parent";
string nameKey = "Name";
string valueKey = "Value";
string x = PreTemplateLogic.ObjectRepository.cntrlName;
string[] CntrlPrpty = ReadObjectrepository();
#>
Console.WriteLine("<#= CntrlPrpty[0] #>");
}
}
}
<#+
public string[] ReadObjectrepository()
{
try
{
string[] properties;
string csvPath = ObjectRepository.xmlFile;
if (File.Exists(ObjectRepository.xmlFile))
{
using (StreamReader reader = new StreamReader(File.OpenRead(csvPath)))
{
string line = reader.ReadLine().ToString();
properties = line.Split(',');
}
return properties;
}
return null;
}
catch (Exception e)
{
throw;
}
}
#>
以下是我想要实现的目标:
- 我需要在运行时生成代码
- 代码的一些参数是从外部文件中获取的(该文件经常更新(
- 在调用此模板时,它需要从文件中获取参数并生成代码
我正在使用自定义主机从另一个解决方案外部调用该模板。ReadcsvtoDictionary
向字典datakey
读取T4模板在运行时所需的一些数据。现在,即使我更新了ReadcsvtoDictionary
读取的文件并再次调用模板,我也没有得到texttemplate
来获取新数据。。
您可以在该模板的分部类中编写ReadObjectrepository方法。
例如,为什么不将ReadObjectrepository((结果传递给模板构造函数中的模板代码?
构造函数也可以在分部类中。
这样调试将是最简单的。