其他分享
首页 > 其他分享> > CodeSmith4-案例

CodeSmith4-案例

作者:互联网

执行子模板

Main.cst

<%@ Template Language="C#" TargetLanguage="Text" %>

<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>


<%@ Property Name="SourceDB" Type="DatabaseSchema" Optional="False" %>
<script runat="template">
public void RenderChild1()
{
    CodeTemplateCompiler compiler = new CodeTemplateCompiler(".\\Child.cst");
    compiler.Compile();
     
    if (compiler.Errors.Count == 0)
    {
        CodeTemplate template = compiler.CreateInstance();
     
        //DatabaseSchema SourceDB = new DatabaseSchema(new MySQLSchemaProvider(), @"Data Source=127.0.0.1;User Id=root;Password=xYi7sR2Ivc;Database=meshop_www;Port=33306;");
        TableSchema table = SourceDB.Tables["user_info"];
     
        template.SetProperty("SourceTable", table);     
        //template.Render(Response);
        template.RenderToFile(".\\"+table.Name+".cs",true);
    }
    else
    {
        for (int i = 0; i < compiler.Errors.Count; i++)
        {
            Console.Error.WriteLine(compiler.Errors[i].ToString());
        }
    }     
}
</script>
<%RenderChild1(); %>

Child.cst

<%@ Template Language="C#" TargetLanguage="Text" %>
<%@ Property Name="SourceTable"  Type="TableSchema" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<script runat="template">
public void Output()
{
    foreach(ColumnSchema column in SourceTable.Columns)
    {
        Response.WriteLine(column.Name);
    }
}
</script>
<% Output(); %>

标签:cst,Errors,CodeSmith4,案例,template,new,table,compiler
来源: https://www.cnblogs.com/fanfan-90/p/15309807.html