开发者

Why my Visual Studio T4 code outputs error?

开发者 https://www.devze.com 2023-02-03 13:09 出处:网络
Following insructions here http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/

Following insructions here http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/

I tried to pass var personName from test1.tt to template1.tt but I can't see no file why ?:

template1.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".txt" #>
<#@ assembly name="System"#>

hello <#= personName #>
开发者_如何学运维

test1.tt

<#@ template language="C#" hostspecific="True" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>

            CallContext.SetData("template1.personName", "Baby");
            ProcessTemplate("template1.tt","testoutput.txt");

<#+
  void ProcessTemplate(string templateFileName, string outputFileName) {
    string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
    string outputFilePath = Path.Combine(templateDirectory, outputFileName);  

    string template = File.ReadAllText(Host.ResolvePath(templateFileName));
    Engine engine = new Engine();
    string output = engine.ProcessTemplate(template, Host);  

    File.WriteAllText(outputFilePath, output);
  }
#>


Oleg's examples use two templates. One to hold the method and a second that imports it to call the methods.

From help example:

ProcessTemplate.tt
<#@ template language="C#" hostspecific="True" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#+
  void ProcessTemplate(string templateFileName, string outputFileName)
  {
    string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
    string outputFilePath = Path.Combine(templateDirectory, outputFileName);  

    string template = File.ReadAllText(Host.ResolvePath(templateFileName));
    Engine engine = new Engine();
    string output = engine.ProcessTemplate(template, Host);  

    File.WriteAllText(outputFilePath, output);
  }
#>

This template also turns on the hostspecific option to generate the Host property. ProcessTemplate method uses this property to determine full path of the standalone template file as well as the output directory. ProcessTemplate method creates a new instance of T4 Engine class, which it uses to compile and run the standalone template.

Here is a template that uses this helper method to generate two output files from two standalone templates." (text from help example)

 Example3.tt
    <#@ include file="ProcessTemplate.tt" #>
    <#
        ProcessTemplate("Standalone1.tt", "StandaloneOutput1.txt");
        ProcessTemplate("Standalone2.tt", "StandaloneOutput2.txt");
    #>

This is the first template that is referenced in the help example. If you specify the error you are getting it will help with debugging.

0

精彩评论

暂无评论...
验证码 换一张
取 消