天天看点

CodeSmith模板引擎系列一

     CodeSmith是一个基于模板的代码生成器,它可以生成任何基于ASCII的编程语言代码。生成的代码可以使用属性进行定制。属性可以是任何具有设计器的.NET对象(大多数.NET内置类型已经有设计器),也可以是一个允许你从结果中有条件地添加或移除代码的简单的boolean 属性,或是一个对象,例如能够访问数据库表信息的TableSchema对象(包括在SchemaExplorer中)。CodeSmith完全可扩展,它允许用户创建定制属性类型。CodeSmith中包括多个定制属性类型的例子,例如,定制一个允许选择XML文件(使用XmlSerializer可将其反序列化到对象中)的属性类型。CodeSmith还允许用户在模板中引用和调用指定的外部程序集并且允许从外部程序集的类生成模板。       我们今天先来看看CodeSmith的固定头信息:
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Description" %> 上面声明了语言为c#(也可以使其他语言)输出为Text非调试状态的模板  <%@ Assembly Name="System.Data" %> 上面Wie引入程序集System.Data相当于我们项目的添加引用  <%@ Import Namespace="System.Data" %> 导入命名空间System.Data相当于我们c#的using <%@ Property Name="NameSpace" Type="String"  Category="Context"  Description="Description" %> 上面为申明字符串的属性NameSpace。这个将会在CodeSmith的Properties框里显示输出参数。
     今天就写一个简单的,毫无意义的模板,根据CodeSmith的SchemaExplorer程序集输出数据库表信息:

代码   

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" %>  

<%@ Assembly Name="SchemaExplorer" %>  

<%@ Import Namespace="SchemaExplorer" %>  

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"  Category="Context" %>  

<%--  -----------------------------------------------------------------  

 -- Date Created:   

 -- Created By:   Generated by Wolf  

 -------------------------------------------------------------------%>  

<html>  

<head>  

    <title>CodeSmith Template</title>      

</head>  

<body>  

    <form id="form1" runat="server">  

    <div>  

 <table style="background-color: #FFFFCC; display: table"    cellpadding="0" cellspacing="0">  

            <tr>  

                <th>  

                    ColumnName</th>  

                    DbType</th>  

                    DataType</th>  

                    Size</th>  

                    IsPrimaryKey</th>  

                    IsForeignKey</th>  

                    Unique</th>  

            </tr>  

            <% foreach(ColumnSchema col in SourceTable.Columns ) {%>  

                <td>  

                    <%= col.Name %></td>  

                    <%= col.NativeType %></td>  

                    <%= col.DataType %></td>  

                   <%= col.Size %></td>  

                    <%= col.IsPrimaryKeyMember %></td>  

                    <%= col.IsForeignKeyMember %></td>  

                    <%= col.IsUnique %></td>  

            <% } %>  

        </table>  

    </div>  

    </form>  

</body>  

</html>  

 我们将输出导出为Html文件结果为:

<a href="http://images.cnblogs.com/cnblogs_com/whitewolf/WindowsLiveWriter/CodeSmith_12E1D/image_2.png" target="_blank"></a>

<a href="http://blog.51cto.com/whitewolfblog/834667#">?</a> <code>今天就写到这里了,这个东西在我们的实际开发中毫无意义,只是拿来作为CodeSmith模板的HelloWorld示例,</code> <code>请别拍砖。</code>  本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/834667,如需转载请自行联系原作者