代码之家  ›  专栏  ›  技术社区  ›  Maslow

T4:在通过脚本提供部分的同时,是否可以使用脚本来填充模板?

  •  0
  • Maslow  · 技术社区  · 15 年前

    我有一个模板,它生成一个类和一个互补的接口,以便从这样的脚本中使用它:

    <#@ template language="C#v3.5" hostspecific="True" debug="True" #>
    <#@ output extension="cs" #>
    <#@ include file="T4Toolbox.tt" #>
    <#@ include file="../BusinessObjectTemplate.tt" #>
    <#
    BusinessObjectTemplate template = new BusinessObjectTemplate();
    template.BusinessName="Priority";
    
    template.PropertyList=new Dictionary<string,BusinessPropertyT4>{
        {"Value",new BusinessPropertyT4("byte")},
        {"Display",new BusinessPropertyT4("string")},
    };
    template.TopRegionText="internal ModelPriority(byte value, String display)\r\n\t\t{\r\n"+
        "\t\t\tValue=value;\r\n"+"\t\t\tDisplay=display;\r\n"+ "\t\t}";
    template.Render();
    #>
    

    我如何生成 TopRegionText (构造器)在不向脚本提供直接字符串的情况下从脚本获取,并将其放入模板中的正确位置?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Oleg Sych    15 年前

    假设您希望使用t4的模板化功能来生成构造函数,您可以在BusinessObjectTemplate类中定义一个虚拟方法(即GeneratePregionText),并从BusinessObjectTemplate.TransformText方法调用它。完成后,您可以这样覆盖它:

    <#+
    class PriorityTemplate: BusinessObjectTemplate
    {
        override void GenerateTopRegionText()
        {
    #>
        internal ModelPriority(byte value, string display)
        {
            Value = value;
            Display = display;
        }        
    <#+
        }
    }
    #>
    

    More here .