zoukankan      html  css  js  c++  java
  • CodeSmith实用技巧(十):通过编程执行模版

    CodeSmith在执行模版时通过调用一些API来完成的,主要经过了以下这几步的操作:

    l         编译一个模版

    l         显示编译错误信息

    l         创建一个新的模版实例

    l         用元数据填充模版

    l         输出结果

    下面这段代码显示了这些操作:

    CodeTemplateCompiler compiler = new CodeTemplateCompiler("..\\..\\StoredProcedures.cst");
    compiler.Compile();
     
    if (compiler.Errors.Count == 0)
    {
        CodeTemplate template 
    = compiler.CreateInstance();
     
        DatabaseSchema database 
    = new DatabaseSchema(new SqlSchemaProvider(), @"Server=(local)\NetSDK;Database=Northwind;Integrated Security=true;");
        TableSchema table 
    = database.Tables["Customers"];
     
        template.SetProperty(
    "SourceTable", table);
        template.SetProperty(
    "IncludeDrop"false);
        template.SetProperty(
    "InsertPrefix""Insert");
     
        template.Render(Console.Out);
    }

    else
    {
        
    for (int i = 0; i < compiler.Errors.Count; i++)
        
    {
            Console.Error.WriteLine(compiler.Errors[i].ToString());
        }

    }


    在这里我们用了Render方法,其实CodeTemplate.RenderToFileCodeTemplate.RenderToString方法可能更有用,它可以直接让结果输出到文件中或赋给字符型的变量。

    注意:该功能只能在CodeSmith专业版中使用
    支持TerryLee的创业产品Worktile
    Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
    https://worktile.com
  • 相关阅读:
    CF1454F Array Partition
    leetcode1883 准时抵达会议现场的最小跳过休息次数
    leetcode1871 跳跃游戏 VII
    leetcode1872 石子游戏VIII
    CF1355C Count Triangles
    CF1245D Shichikuji and Power Grid
    CF1368C Even Picture
    CF1368D AND, OR and square sum
    CF1395C Boboniu and Bit Operations
    SpringBoot和开发热部署
  • 原文地址:https://www.cnblogs.com/Terrylee/p/306236.html
Copyright © 2011-2022 走看看