代码生成模板未更新文件中的参数

本文关键字:参数 文件 更新 代码生成 | 更新日期: 2023-09-27 17:59:40

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

以下是我想要实现的目标:

  1. 我需要在运行时生成代码
  2. 代码的一些参数是从外部文件中获取的(该文件经常更新(
  3. 在调用此模板时,它需要从文件中获取参数并生成代码

我正在使用自定义主机从另一个解决方案外部调用该模板。ReadcsvtoDictionary向字典datakey读取T4模板在运行时所需的一些数据。现在,即使我更新了ReadcsvtoDictionary读取的文件并再次调用模板,我也没有得到texttemplate来获取新数据。。

代码生成模板未更新文件中的参数

您可以在该模板的分部类中编写ReadObjectrepository方法。

例如,为什么不将ReadObjectrepository((结果传递给模板构造函数中的模板代码?

构造函数也可以在分部类中。

这样调试将是最简单的。