AppleStar

  • Increase font size
  • Default font size
  • Decrease font size
首页 Developer DotNet CodeSmith简介

CodeSmith简介

E-mail 打印 PDF
创建好一个模板后第一步要指明这是一个C#语言的模板。
<%@ CodeTemplate Language="C#" TargetLanguage="C#"
      Deion
="Generates a class including a special informational header" %>

        第二步,我们要指明模板在生成代码时的属性,即生成代码需要的输入值变量。

<%@ Property Name="NameSpace" Type="String"
      Category
="Context"
      Deion
="The namespace to use for this class" %>
        如上边所示,在进行代码生成时,在CodeSmith Explorer中选择模板后生成代码的窗口中,变量的名称为NameSpace,类型是String,类别是Context,当用户选中这个属性时对于属性的描述Deion。
        我们可以按照C#语言的语法去使用定义的变量,例如:
///////////////////////////////////////////////////////////////////////////////////////
//  <%=ClassName%>.cs

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


然后我们在CodeSmith Explorer中双击这个模板就会看到相应的属性界面,这里的属性均是我们在前边定义的属性。

按下Generate按钮生成,即可实现一个简单的类代码的生成。

 1///////////////////////////////////////////////////////////////////////////////////////
 2//  MyClass.cs
 3// Deion: 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 deion 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的目的就是为了快速高效的开发。
最后更新于: 2006-11-07 11:34  

添加评论


验证码
刷新

用户登录

ADS