最新版CodeSmith3.2(.net2.0)教程之四:生成属于你的第一个模板

来源:互联网 发布:七彩云南淘宝店真的吗 编辑:程序博客网 时间:2024/05/18 10:36

我们回顾一下上一节的内容:我们知道了创建模板首先应该仔细观察我们所要的模板需要生成的代码,把代码分为三个部分:静态,系统自动生成,动态的内容。下面我们就来生成我们的第一个模板:

1.模板中的静态内容



      添加静态的内容到CodeSmith 模板里非常容易。CodeSmith 会不加变化的直接输出动态代码标记以外的内容。所以我们第一步就来先学习怎样生成一个没有任何变化直接输出的模板:

 1<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create an AssemblyInfo.cs file." %>
 2using System.Reflection;
 3using System.Runtime.CompilerServices;
 4//
 5// Created: Friday, July 1, 2005
 6// Author:  Alan Maxwell
 7//
 8[assembly: AssemblyTitle("User storage utility")]
 9[assembly: AssemblyDescription("Helps manage data in Isolated Storage files.")]
10[assembly: AssemblyConfiguration("Retail")]
11[assembly: AssemblyCompany("MegaUtilities, Inc.")]
12[assembly: AssemblyProduct("StorageScan")]
13[assembly: AssemblyCopyright("Copyright (c) 2005 MegaUtilities, Inc.")]
14[assembly: AssemblyCulture("")]
15[assembly: AssemblyVersion("1.0.*")]
16[assembly: AssemblyFileVersion("1.0")]
17[assembly: AssemblyDelaySign(true)]


现在你可以运行模板生成代码。发现了什么?CodeSmith返回了一模一样的原文件,这是因为模板里面没有包含动态内容。接下来我们就可以利用CodeSmith的强大功能加入动态内容以及交互数据。

2.加入动态内容


     接着让我们在模板里加入CodeSmith 可以自动生成的部分,我们将使用和ASP.NET里一样的标记符在模板里加入一些C# 代码。CodeSmith将把<%= 和 %> 标记符中间的部分作为计算表达式,并在生成代码时运行。运行的结果将在相应位置插入到生成的代码中。

下面的模板中包含了两个时间表达式(红色部分)

 1<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create an AssemblyInfo.cs file." %>
 2using System.Reflection;
 3using System.Runtime.CompilerServices;
 4//
 5// Created: <%= DateTime.Now.ToLongDateString() %>
 6// Author:  Alan Maxwell
 7//
 8[assembly: AssemblyTitle("User storage utility")]
 9[assembly: AssemblyDescription("Helps manage data in Isolated Storage files.")]
10[assembly: AssemblyConfiguration("Retail")]
11[assembly: AssemblyCompany("MegaUtilities, Inc.")]
12[assembly: AssemblyProduct("StorageScan")]
13[assembly: AssemblyCopyright("Copyright (c) <%= DateTime.Now.Year.ToString() %> MegaUtilities, Inc.")]
14[assembly: AssemblyCulture("")]
15[assembly: AssemblyVersion("1.0.*")]
16[assembly: AssemblyFileVersion("1.0")]
17[assembly: AssemblyDelaySign(true)]


现在,代码创建日期和版权期将自动的加在生成的代码的相应位置。但是还有另外一部分CodeSmith不能自动生成,比如assembly 标题或者assembly 的描述性文字,因为这些都是随文件不同根据用户需要而改变的,诸如此类的可以由CodeSmith 属性生成。

 

3.加入属性



     CodeSmith使用属性定义了模板的元数据。你需要使用属性来代替需要用户自己定义输出的内容。接下来我们将向AssemblyInfo.cst模板里按我们的需要添加属性:

 1<%@ Property Name="Author" Type="System.String" Description="Lead author of the project." %>
 2<%@ Property Name="Title" Type="System.String" Description="Title of the project." %>
 3<%@ Property Name="Description" Type="System.String" Description="Description of the project." %>
 4<%@ Property Name="Configuration" Type="System.String" Default="Debug" Description="Project configuration." %>
 5<%@ Property Name="Company" Type="System.String" Default="MegaUtilities, Inc." %>
 6<%@ Property Name="Product" Type="System.String" Description="Product Name." %>
 7<%@ Property Name="Version" Type="System.String" Default="1.0.*" Description=".NET assembly version." %>
 8<%@ Property Name="FileVersion" Type="System.String" Default="1.0" Description="Win32 file version." %>


  每个属性都有一个名字,以及类型。有些属性还有默认值,当然默认值不是必需的。大多数属性还有描述,为了说明这个属性的作用。当用户在模板里使用某个属性的时候,CodeSmith 会显示其描述来帮你输入正确的值。

 

4.在模板中使用属性



     在模板中插入属性值也是使用<%= 和 %>来标记,不过在标记中间要填的是属性的名称。下面就是我们最终模板:

 1<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create an AssemblyInfo.cs file." %>
 2<%@ Property Name="Author" Type="System.String" Description="Lead author of the project." %>
 3<%@ Property Name="Title" Type="System.String" Description="Title of the project." %>
 4<%@ Property Name="Description" Type="System.String" Description="Description of the project." %>
 5<%@ Property Name="Configuration" Type="System.String" Default="Debug" Description="Project configuration." %>
 6<%@ Property Name="Company" Type="System.String" Default="MegaUtilities, Inc." %>
 7<%@ Property Name="Product" Type="System.String" Description="Product Name." %>
 8<%@ Property Name="Version" Type="System.String" Default="1.0.*" Description=".NET assembly version." %>
 9<%@ Property Name="FileVersion" Type="System.String" Default="1.0" Description="Win32 file version." %>
10using System.Reflection;
11using System.Runtime.CompilerServices;
12//
13// Created: <%= DateTime.Now.ToLongDateString() %>
14// Author:   <%= Author %>
15//
16[assembly: AssemblyTitle("<%= Title %>")]
17[assembly: AssemblyDescription("<%= Description %>")]
18[assembly: AssemblyConfiguration("<%= Configuration %>")]
19[assembly: AssemblyCompany("<%= Company %>")]
20[assembly: AssemblyProduct("<%= Product %>")]
21[assembly: AssemblyCopyright("Copyright (c) <%= DateTime.Now.Year.ToString() %> <%= Company %>")]
22[assembly: AssemblyCulture("")]
23[assembly: AssemblyVersion("<%= Version %>")]
24[assembly: AssemblyFileVersion("<%= FileVersion %>")]
25[assembly: AssemblyDelaySign(true)]


注:一个属性可以重复在多个不同的地方使用

 

现在模板看起来就比你之前的原文件复杂多了,但是记住:最重要的是你只用写一次模板,你就可以生成无数个代码,你因为书写动态内容和属性而多花的时间将在以后使用模板生成代码节约的时间得到回报。

5.编译模板并生成代码


    现在,模板就可以使用啦。保存该文件,并双击窗口浏览器。将打开模板属性页。在属性中输入相应的值点生成按钮,生成一个新的AssemblyInfo.cs文件



  看到没有,是不是比你去修改那些文件容易多了?

  算一下使用模板后你创建一个新的项目而节约的时间。你就会好不犹豫的选择CodeSmith 。只要你按我的方法来建模板:

(1)先从观察你要输出的代码开始
(2)确定出静态内容,动态内容,自动生成的内容
(3)为动态内容添加属性

你可以为web页面,数据层,用户接口,以及任何你想象得到得创建你想要的模板。CodeSmith会让你摆脱了枯燥乏味的手工输入代码时代,极大地提高你的工作效率。