CodeSmith基础(一)
2005-12-19 13:57 努力学习的小熊 阅读(64528) 评论(43) 编辑 收藏 举报
请大家耐心看完所有的基础文章,前两篇网上发表的比较多,是CodeSmith英文帮助文档的第一篇,我后面写的基础是将其他所有的英文帮助全部翻译出来了,全部为本人手写翻译,希望对大家有所帮助
创建好一个模板后第一步要指明这是一个C#语言的模板。
<%@ CodeTemplate Language="C#" TargetLanguage="C#"
Description="Generates a class including a special informational header" %>
第二步,我们要指明模板在生成代码时的属性,即生成代码需要的输入值变量。
我们可以按照C#语言的语法去使用定义的变量,例如:
例如下面这个例子模板使用了上面介绍的知识。Test.cst
生成后的代码即可放入Visual Studio .NET中使用,我们使用CodeSmith的目的就是为了快速高效的开发。

创建好一个模板后第一步要指明这是一个C#语言的模板。


第二步,我们要指明模板在生成代码时的属性,即生成代码需要的输入值变量。
<%@ Property Name="NameSpace" Type="String"
Category="Context"
Description="The namespace to use for this class" %>
如上边所示,在进行代码生成时,在CodeSmith Explorer中选择模板后生成代码的窗口中,变量的名称为NameSpace,类型是String,类别是Context,当用户选中这个属性时对于属性的描述Description。Category="Context"
Description="The namespace to use for this class" %>
我们可以按照C#语言的语法去使用定义的变量,例如:
///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs
// 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" %>
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
//
}
}
}
// 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按钮生成,即可实现一个简单的类代码的生成。
1
///////////////////////////////////////////////////////////////////////////////////////
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
11
using System;
12
13
namespace 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

生成后的代码即可放入Visual Studio .NET中使用,我们使用CodeSmith的目的就是为了快速高效的开发。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)