课程表

CodeSmith课程

工具箱
速查手册

CodeSmith 第一个代码模板

当前位置:免费教程 » 软件/图像 » CodeSmith

CodeSmith 概述我们通过使用 CodeSmith 从数据库自动生成 NHiberate 代码,可以了解到使用 CodeSmith 自动生成代码的基本步骤:

  1. 选择使用合适的模板,CodeSmith 随开发包自带了大量常用的模板,如果找不到合适的模板,CodeSmith 支持自定义模板。
  2. 为模板选择合适的参数设置。
  3. 自动生成代码(可以为任意类型的代码,C#,Java, .XML 文本等)

第8张

其核心为代码模板文件,随 CodeSmith 自带了不少常用的模板,可以通过模板浏览器来查询,此外网上也有很多第三方开发的模板,在使用前可以先查查是否已有现成的模板,或是可以通过修改现有的模板来完成自动生成代码的需要。

在开发应用时,很多人都喜欢通过复制以前的项目中的代码,然后通过修改以满足新项目,这些重用的代码通常具有很多共性(可以想想 C++ 的模板类,C# 的 Generic 等),CodeSmith 就是用来为这些具有相似性的代码创建模板,然后通过设置属性(代码直接的不同点),就可以自动创建所需代码。

本例通过一个简单的例子来介绍创建一个自定义代码模板的方法。CodeSmith 提供了 Visual Studio的集成开发环境的支持,本例也是通过创建模板自动生成简化每个的 C# 项目都需要的 AssemblyInfo.cs,在开发 C# 应用时,一般是通过手工修改 AssemblyInfo.cs 的中属性(或者是 Copy & Paste :-)).

首先我们使用 Visual Studio 创建一个 C# HelloWorld 下面(Console 或是 WinForm 项目都可以),可以打开项目中的 AssemblyInfo.cs

  1. using System.Reflection;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. // General Information about an assembly is controlled through the following
  5. // set of attributes. Change these attribute values to modify the information
  6. // associated with an assembly.
  7. [assembly: AssemblyTitle("HelloWorld")]
  8. [assembly: AssemblyDescription("")]
  9. [assembly: AssemblyConfiguration("")]
  10. [assembly: AssemblyCompany("Microsoft")]
  11. [assembly: AssemblyProduct("HelloWorld")]
  12. [assembly: AssemblyCopyright("Copyright © Microsoft 2013")]
  13. [assembly: AssemblyTrademark("")]
  14. [assembly: AssemblyCulture("")]
  15. // Setting ComVisible to false makes the types in this assembly not visible
  16. // to COM components. If you need to access a type in this assembly from
  17. // COM, set the ComVisible attribute to true on that type.
  18. [assembly: ComVisible(false)]
  19. // The following GUID is for the ID of the typelib if this project is exposed to COM
  20. [assembly: Guid("72797715-64b9-4bab-a49f-f55e8a0a18d7")]
  21. // Version information for an assembly consists of the following four values:
  22. //
  23. // Major Version
  24. // Minor Version
  25. // Build Number
  26. // Revision
  27. //
  28. // You can specify all the values or you can default the Build and Revision Numbers
  29. // by using the '*' as shown below:
  30. // [assembly: AssemblyVersion("1.0.*")]
  31. [assembly: AssemblyVersion("1.0.0.0")]
  32. [assembly: AssemblyFileVersion("1.0.0.0")]

为了使用 CodeSmith,我们在 HelloWorld 中添加 CodeSmith 的项目文件并创建一个模板文件AssemblyInfo.cst

第9张

创建好的项目文件如下:

第10张

编写 CodeSmith 的代码模板和编写 Asp.Net 的 Page 非常类似,CodeSmith 支持以 C#,VB.Net和 JavaScript 做为脚本语言来编写模板,本例使用 C# 做为脚本语言(源代码/语言),计划生成的也是 C# 语言(目标代码/语言),打开 AssemblyInfo.cst,修改代码为

  1. <%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create an AssemblyInfo.cs file." %>

每个 CodeSmith 的代码模板都是以 CodeTemplate 开始,定义代码模板使用的源语言,目标语言和简单的描述。

然后将这个模板添加到 CodeSmith 项目中,可以右键单击 codesmith.csp ,选择 Add output

第11张

这时 CodeSmith 的项目将创建好了,但单击”Generate code”不会生成任何代码,因为我们的代码模板 AssemblyInfo.cst 没做任何事。

创建代码模板可以从生成的结果开始,可以直接先把要生成的代码复制到代码模板 AssemblyInfo.cst中,比如:

  1. using System.Reflection;
  2. using System.Runtime.CompilerServices;
  3. //
  4. // Created: 1/1/2013
  5. // Author: James Shen
  6. //
  7. [assembly: AssemblyTitle("User storage utility")]
  8. [assembly: AssemblyDescription("Helps manage data in Isolated Storage files.")]
  9. [assembly: AssemblyConfiguration("Retail")]
  10. [assembly: AssemblyCompany("Guidebee Pty Ltd, Inc.")]
  11. [assembly: AssemblyProduct("StorageScan")]
  12. [assembly: AssemblyCopyright("Copyright (c) Guidebee Pty Ltd.")]
  13. [assembly: AssemblyCulture("")]
  14. [assembly: AssemblyVersion("1.0.*")]
  15. [assembly: AssemblyFileVersion("1.0")]
  16. [assembly: AssemblyDelaySign(true)]

可以把要生成的代码模板的内容分成三部分:

  • 固定内容
  • 可以通过代码动态生成的部分(如上面的日期)
  • 需要用户提供属性配置的部分

此时如果使用 Codesmith 的 Generate Codes, 将自动生成 AssemblyInfo.cs (缺省为模板名),不过 AssemblyInfo.cs 位置不是我们所需的 Properties/AssemblyInfo.cs, 这可以通过重载代码模板的 GetFileName 方法来实现:

  1. <%@ CodeTemplate Language="C#" TargetLanguage="C#"
  2. Description="Create an AssemblyInfo.cs file." %>
  3. ...
  4. <script runat="template">
  5. public override string GetFileName() {
  6. return "Properties/AssemblyInfo.cs";
  7. }
  8. </script>

这样在使用 CodeSmith 项目的 Generate Codes,就自动覆盖原来的 Properties/AssemblyInfo.cs 文件。 内容就是模板中的代码部分。

但每次生成的代码都是固定的,作为模板来说没有什么灵活性,下面我们可以通过检查模板的内容,觉定那些内容是可变的。比如 AssemblyInfo.cs 的日期和 Assembly 的各个属性对于不同的项目来说是可变的。

这些可变的内容其中一部分可以通过代码自动生成(如日期),有一部分需要用户来配置,比如AssemblyTitle,AssemblyDescription 等。

对于日期部分可以通过C#代码实现如下:

  1. // Created: <%= DateTime.Now.ToLongDateString() %>

可以看出来 CodeSmith 的模板文件如 AssemblyInfo.cst 和 Asp.Net 的 Page 文件中功能是非常类似,可以通过<%= 和%>直接嵌入 C# 代码(或 VB.Net,JavaScripts)。

对于属性来说,可以通过先定义属性:

  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="Guidebee Pty Ltd." %>
  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." %>

属性定义通过 Property 定义,Name 定义属性名,Type 为属性的数据类型,Default 定义属性的缺省值, Description 可以定义属性的作用及说明。

然后就可以在 C# 代码中使用这些属性,完整的代码模板如下:

  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="Guidebee Pty Ltd." %>
  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." %>
  10. using System.Reflection;
  11. using 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)]

此时如果需要“Generate output” 首先要配置代码模板的属性,这通过”Manage output” 来完成,

第12张

次数如果打开 codesmith.csp 文件可以看到为 AssemblyInfo.cst 配置的属性内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd">
  3. <propertySets>
  4. <propertySet name="AssemblyInfo" template="AssemblyInfo.cst">
  5. <property name="Configuration">Debug</property>
  6. <property name="Company">Guidebee Pty Ltd.</property>
  7. <property name="Version">1.0.*</property>
  8. <property name="FileVersion">1.0</property>
  9. <property name="Author">James Shen</property>
  10. <property name="Title">Code smith Demo</property>
  11. <property name="Description">My First Template code</property>
  12. <property name="Product">SandCastle</property>
  13. </propertySet>
  14. </propertySets>
  15. </codeSmith>

生成代码如下:

  1. using System.Reflection;
  2. using System.Runtime.CompilerServices;
  3. //
  4. // Created: Thursday, 3 January 2013
  5. // Author: James Shen
  6. //
  7. [assembly: AssemblyTitle("Code smith Demo")]
  8. [assembly: AssemblyDescription("My First Template code")]
  9. [assembly: AssemblyConfiguration("Debug")]
  10. [assembly: AssemblyCompany("Guidebee Pty Ltd.")]
  11. [assembly: AssemblyProduct("SandCastle")]
  12. [assembly: AssemblyCopyright("Copyright (c) 2013 Guidebee Pty Ltd.")]
  13. [assembly: AssemblyCulture("")]
  14. [assembly: AssemblyVersion("1.0.*")]
  15. [assembly: AssemblyFileVersion("1.0")]
  16. [assembly: AssemblyDelaySign(true)]
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号