CodeSmith模板(一)

开始学习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格式。

posted on 2011-01-30 08:24  老老狼  阅读(548)  评论(0编辑  收藏  举报

导航