開始學習CodeSmith模板。
首先,是基本模板:
<%@ 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
//
}
}
}
該模板定義了三個屬性,這三個屬性如果直接Execute,Explorer裏就會出現三個屬性,點擊“生成”,右邊會生成C#代碼。
有個小技巧,如何讓Codesmith支持中文:
1、Studio裏,設置Tools-->Options,對話框裏面Enable Unicode support複選框勾選上;
2、模板里加入:ResponseEncoding="UTF-8",修改後的代碼諸如:<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Generate Model Class for a specific Data Table" %>
3、用記事本打開cst文件,另存爲utf-8格式。