創建好一個模板後第一步要指明這是一個C#語言的模板。
Description="Generates a class including a special informational header" %>
第二步,我們要指明模板在生成代碼時的屬性,即生成代碼需要的輸入值變量。
Category="Context"
Description="The namespace to use for this class" %>
我們可以按照C#語言的語法去使用定義的變量,例如:
// File: <%=ClassName%>.cs
例如下面這個例子模板使用了上面介紹的知識。Test.cst
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中雙擊這個模板就會看到相應的屬性界面,這裏的屬性均是我們在前邊定義的屬性。
按下Generate按鈕生成,即可實現一個簡單的類代碼的生成。
2// File: MyClass.cs
3// Description: Enter summary here after generation.
4// ---------------------
5// Copyright © 2003 Our Client
6// ---------------------
7// History
8// 12/2/2003 Mr. Smith Original Version
9///////////////////////////////////////////////////////////////////////////////////////
10
11using System;
12
13namespace MyNameSpace
14{
15 /// <summary>
16 /// Summary description for MyClass.
17 /// </summary>
18 public class MyClass
19 {
20 public MyClass()
21 {
22 //
23 // TODO: Add constructor logic here
24 //
25 }
26 }
27}
生成後的代碼即可放入Visual Studio .NET中使用,我們使用CodeSmith的目的就是爲了快速高效的開發。