CodeSmith基礎(一) -------- 轉自博客園

 請大家耐心看完所有的基礎文章,前兩篇網上發表的比較多,是CodeSmith英文幫助文檔的第一篇,我後面寫的基礎是將其他所有的英文幫助全部翻譯出來了,全部爲本人手寫翻譯,希望對大家有所幫助49.gif

        創建好一個模板後第一步要指明這是一個C#語言的模板。
none.gif<%@ CodeTemplate Language="C#" TargetLanguage="C#"
none.gif      Description
="Generates a class including a special informational header" %>

        第二步,我們要指明模板在生成代碼時的屬性,即生成代碼需要的輸入值變量。

<%@ Property Name="NameSpace" Type="String"
      Category
="Context"
      Description
="The namespace to use for this class" %>
        如上邊所示,在進行代碼生成時,在CodeSmith Explorer中選擇模板後生成代碼的窗口中,變量的名稱爲NameSpace,類型是String,類別是Context,當用戶選中這個屬性時對於屬性的描述Description。
        我們可以按照C#語言的語法去使用定義的變量,例如:
///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs

        例如下面這個例子模板使用了上面介紹的知識。Test.cst
<%@ CodeTemplate Language="C#" TargetLanguage="C#"
      Description
="Generates a class including a special informational header" %>
 
<%@ Property Name="NameSpace" Type="String"
      Category
="Context"
      Description
="The namespace to use for this class" %>
 
<%@ Property Name="ClassName" Type="String"
      Category
="Context"
      Description
="The name of the class to generate" %>
 
<%@ Property Name="DevelopersName" Type="String"
      Category
="Context"
      Description
="The name to include in the comment header" %>
///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright © <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
//    <%= DateTime.Now.ToShortDateString() %>    <%= DevelopersName%>    Original Version
///////////////////////////////////////////////////////////////////////////////////////
 
using System;
 
namespace <%=NameSpace %>
{
      
/// <summary>
      
/// Summary description for <%=ClassName %>.
      
/// </summary>
      public class <%=ClassName %>
      {
            
public <%=ClassName %>()
            {
                  
//
                  
// TODO: Add constructor logic here
                  
//
            }
      }
}


然後我們在CodeSmith Explorer中雙擊這個模板就會看到相應的屬性界面,這裏的屬性均是我們在前邊定義的屬性。
codesmith基礎1.jpg

按下Generate按鈕生成,即可實現一個簡單的類代碼的生成。

 1expandedblockstart.gif///////////////////////////////////////////////////////////////////////////////////////
 2none.gif// File: MyClass.cs
 3none.gif// Description: Enter summary here after generation.
 4none.gif// ---------------------
 5none.gif// Copyright © 2003 Our Client
 6none.gif// ---------------------
 7none.gif// History
 8none.gif//    12/2/2003    Mr. Smith    Original Version
 9expandedblockstart.gif///////////////////////////////////////////////////////////////////////////////////////
10none.gif 
11none.gifusing System;
12none.gif 
13none.gifnamespace MyNameSpace
14expandedblockstart.gif{
15expandedsubblockstart.gif      /// <summary>
16inblock.gif      /// Summary description for MyClass.
17expandedsubblockend.gif      /// </summary>

18inblock.gif      public class MyClass
19expandedsubblockstart.gif      {
20inblock.gif            public MyClass()
21expandedsubblockstart.gif            {
22inblock.gif                  //
23inblock.gif                  // TODO: Add constructor logic here
24inblock.gif                  //
25expandedsubblockend.gif            }

26expandedsubblockend.gif      }

27expandedblockend.gif}

生成後的代碼即可放入Visual Studio .NET中使用,我們使用CodeSmith的目的就是爲了快速高效的開發。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章