开发者

T4: Is it possible to use a script to fill in a template, while still providing sections via the script?

I have a template that generates a class and a complementary interface to go with it from a script like so:

<#@ template language="C#v3.5" hostspecific="True" debug="True" #>
<#@ output extension="cs" #&开发者_StackOverflow中文版gt;
<#@ 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();
#>

How would I generate the TopRegionText(constructor) from the script without feeding it a direct string and have it go into the right place in the template?


Assuming that you would prefer to use templating functionality of T4 to generate the constructor, you can define a virtual method (i.e. GenerateTopRegionText) in BusinessObjectTemplate class and call it from BusinessObjectTemplate.TransformText method. Having done that, you can override it like so:

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

More here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜