簡體   English   中英

如何將字符串參數傳遞給 t4 模板

[英]How do I pass a string parameter to a t4 template

嗨,我正在嘗試找到一種將普通字符串作為參數傳遞給文本模板的方法。

這是我的模板代碼,如果有人能告訴我我需要在 c# 中編寫什么來傳遞我的參數並創建類文件。 那會很有幫助,謝謝。

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ parameter name="namespacename" type="System.String" #>
<#@ parameter name="classname" type="System.String" #>
<#
this.OutputInfo.File(this.classname);
#>
namespace <#= this.namespacename #>
{
    using System;
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Xml; 

    /// <summary>
    /// This class describes the data layer related to <#= this.classname #>.
    /// </summary>
    /// <history>
    ///   <change author=`Auto Generated` date=<#= DateTime.Now.ToString("dd/MM/yyyy") #>>Original Version</change>
    /// </history>
    public partial class <#= this.classname #> : DataObject
    {
        #region constructor

        /// <summary>
        /// A constructor which allows the base constructor to attempt to extract the connection string from the config file.
        /// </summary>
        public <#= this.classname #>() : base() {}

        /// <summary>
        /// A constructor which delegates to the base constructor to enable use of connection string.
        /// </summary>
        /// <param name='connectionstring`></param>
        public <#= this.classname #>(string connectionstring) : base(connectionstring) {}

        #endregion
    }
}

以下是傳遞參數的一種方式:

  1. 您必須創建 TextTemplatingSession。
  2. 為參數設置會話字典。
  3. 使用該會話處理模板。

示例代碼(將 ResolvePath 替換為 tt 文件的位置):

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".txt" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<# 
string templateFile = this.Host.ResolvePath("ClassGeneration.tt");
string templateContent = File.ReadAllText(templateFile);

TextTemplatingSession session = new TextTemplatingSession();
session["namespacename"] = "MyNamespace1";
session["classname"] = "MyClassName";

var sessionHost = (ITextTemplatingSessionHost) this.Host;
sessionHost.Session = session;

Engine engine = new Engine();
string generatedContent = engine.ProcessTemplate(templateContent, this.Host);

this.Write(generatedContent);  #>

我在 Oleg Sych 的博客上看到了這個例子,這是 t4 的重要資源。 這是更新的鏈接: https : //web.archive.org/web/20160706191316/http : //www.olegsych.com/2010/05/t4-parameter-directive

這是一個 3 年前的問題,我不知道模板庫已經發展了多少,以及我對問題的解決方案是否適用於舊版本的 Visual Studio 和/或 .NET 等。我目前使用的是 Visual Studio 2015和 .NET 4.6.1。

概括

使用“類功能控制塊”向模板生成的類聲明公共成員,並在模板文本中引用這些公共成員。

例子

在 C# 項目中,選擇“添加新項”>“運行時文本模板”>“Salutation.tt”。 您將獲得一個具有以下默認聲明的新 .tt 文件:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>

在聲明下方輸入您的模板文本:

My name is <#= Name #>.
<# if (RevealAge) { #>
I am <#= Age #> years old.
<# } #>

在 .tt 文件的末尾,將您的參數作為公共類成員添加到“類功能控制塊”中。 這個塊必須到文件的末尾

<#+
public string Name { get; set; }
public int Age { get; set; }
public bool RevealAge = false;
#>

然后,例如,在控制台應用程序中,您可以按如下方式使用您的模板:

Console.Write(new Salutation
{
    Name = "Alice",
    Age = 35,
    RevealAge = false
}.TransformText());

Console.Write(new Salutation
{
    Name = "Bob",
    Age = 38,
    RevealAge = true
}.TransformText());

並得到以下輸出:

My name is Alice.
My name is Bob.
I am 38 years old.
Press any key to continue . . .

有關 T4 語法的更多信息,請參閱 MSDN 文章編寫 T4 文本模板

還可以在處理模板內容之前將參數注入到模板內容中。 為此,請將<##>添加到您的模板中作為代碼注入的占位符。

GenericClass.ttinclude

<#@ template language="C#" #>
<##>
namespace <#= Namespace #>
{
    public class <#= ClassName #> : IGeneric<<#= TypeParameter #>>
    {
        public <#= TypeParameter #> ReturnResult() => 1 + 3;
    }
}
<#+
    public string ClassName { get; set; }
    public string Namespace { get; set; }
    public string TypeParameter { get; set; }
#>

添加用於導入其他模板的通用模板( Generator.ttinclude ):

<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#+
    public const string ParametersInjectionPattern = "<" + "#" + "#" + ">";

    public void Generate(string baseTemplatePath, IDictionary<string, string> parameters)
    {
        var template = File.ReadAllText(this.Host.ResolvePath(baseTemplatePath));

        template = template.Replace(ParametersInjectionPattern, GenerateParametersAssignmentControlBlock(parameters));

        this.Write(new Engine().ProcessTemplate(template, this.Host).Trim());
    }

    public string GenerateParametersAssignmentControlBlock(IDictionary<string, string> parameters)
    {
        var sb = new StringBuilder();
        sb.Append('<').Append('#').Append(' ');

        foreach (var parameter in parameters)
            sb.Append($"{parameter.Key} = {parameter.Value};");

        sb.Append(' ').Append('#').Append('>');
        return sb.ToString();
    }

    public string SurroundWithQuotes(string str)
    {
        return $"\"{str}\"";
    }

    public string GetTemplateName()
    {
        return Path.GetFileNameWithoutExtension(this.Host.TemplateFile).Replace(".Generated", "");
    }
#>

然后在任何特定模板中使用它,您可以在其中傳遞必要的參數( UInt64Class.Generated.tt ):

<#@ template hostspecific="true" language="C#" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#@ include file="Generator.ttinclude" #>
<#
    var parameters = new Dictionary<string, string>()
    {
        { "Namespace", SurroundWithQuotes("T4GenericsExample") },
        { "ClassName", SurroundWithQuotes(GetTemplateName()) },
        { "TypeParameter", SurroundWithQuotes("System.UInt64") }
    };

    Generate("GenericClass.ttinclude", parameters);
#>

完整的示例可以在https://github.com/Konard/T4GenericsExample找到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM