C# 代码生成模板不更新文件中的参数 使用制度; 使用System.Collections.Generic; 命名空间模板逻辑 { 公共部分类GeneratedClass { 字典数据键=新字典(); public void GenerateParentContainer(字符串cntrlName) { 控制台。写线(“”); } } }
以下是我想要实现的目标:C# 代码生成模板不更新文件中的参数 使用制度; 使用System.Collections.Generic; 命名空间模板逻辑 { 公共部分类GeneratedClass { 字典数据键=新字典(); public void GenerateParentContainer(字符串cntrlName) { 控制台。写线(“”); } } },c#,.net,visual-studio-2015,code-generation,t4,C#,.net,Visual Studio 2015,Code Generation,T4,以下是我想要实现的目标: 我需要在运行时生成代码 代码的一些参数是从外部文件(经常更新)获取的 调用此模板时,它需要从文件中获取参数并生成代码 我正在使用自定义主机从另一个解决方案外部调用模板。ReadcsvtoDictionary将一些数据读取到运行时T4模板所需的字典datakey。 现在,即使我更新了由ReadcsvtoDictionary读取的文件并再次调用模板,我也不会获取textmplate来获取新数据。您可以在分部类中为该模板编写ReadObjectrepository方法 例如,
ReadcsvtoDictionary
将一些数据读取到运行时T4模板所需的字典datakey
。
现在,即使我更新了由
ReadcsvtoDictionary
读取的文件并再次调用模板,我也不会获取textmplate
来获取新数据。您可以在分部类中为该模板编写ReadObjectrepository方法
例如,为什么不将ReadObjectrepository()结果传递给模板构造函数中的模板代码
构造函数也可以在分部类中
这样,调试将是最简单的。您可以在分部类中为此模板编写ReadObjectrepository方法 例如,为什么不将ReadObjectrepository()结果传递给模板构造函数中的模板代码 构造函数也可以在分部类中
用这种方法调试将是最简单的。您在这里试图解决的问题是什么?忽视你试图解决问题的努力,这就是我想要实现的。。1.我需要在运行时生成代码。2.代码的一些参数是从外部文件(经常更新)获取的。3.调用此模板时,它需要从文件中获取参数并生成代码。请提供帮助??您试图解决的问题是什么?忽视你试图解决问题的努力,这就是我想要实现的。。1.我需要在运行时生成代码。2.代码的一些参数是从外部文件(经常更新)获取的。3.调用此模板时,它需要从文件中获取参数并生成代码。是否需要帮助??
<#@ 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:\Users\ic019672\Documents\visual studio 2015\Projects\PreTemplateLogic\PreTemplateLogic\bin\Debug\ReadfromCSV.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;
}
}
#>