Codesmith使用技巧

CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器,程序可以自定义模板,从而减少重复编码的劳动量,提高效率。 
下面我们就来学习一下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" %>


using System;
namespace <%=NameSpace %>
      {
/// <summary>
/// Summary description for <%=ClassName %>.
/// </summary>
public class <%=ClassName %>
{
public <%=ClassName %>()
{
//
// TODO: Add constructor logic here
//
}
}
}
解析:

第一句:

这个是模板中唯一必须的声明,包含一些模板特殊的属性,包含模板使用的语言、生成的语言和一些对于模板的描述。

CodeTemplate代表代码的模板,Language是我们要指定模板的语言,TargetLanguage我们要指定的模板应用的目标语言,一般与language一致。

Description描述信息。

第二句:

<%@ Property Name="NameSpace" Type="String"

Category="Context"

Description="The namespace to use for this class" %>

Property:指定我们要声明的属性,Name指定当前属性的名称,Type指定当前属性的类型,Category:用来说明这个属性在CodeSmith Explorer的属性面板中显示成什么类型,例

如下拉选择、直接输入等,Description是指定当前属性的描述。

第三句:第三句是C#语言的定义了,在定义这些语句的时候只要我们在需要做成模板进行替换的地方写上<%=属性名 %>就行了。这样一个超级简单的模板就出来了。

运行模板文件的效果如图:



在使用模板的时候,我们通常都能看到下拉列表的选择,那么我们应该怎么去编写带有下拉列表的模板呢?其实很简单,先定义一个枚举类型的变量,然后将属性的类型设置为枚举型的就可以了。下面我们来看一看例子:

[plain]  view plain copy print ?
  1. <%@ Property Name="CollectionType" Type="CollectionTypeEnum" Category="Collection" Description="Type of collection" %>  
  2. <script runat="template">  
  3. public enum CollectionTypeEnum  
  4. {  
  5.     value1,  
  6.     value2,  
  7.     value3  
  8. }  
  9. </script>  
在脚本标签<script>中必须包含这个参数runat=”template”,否则他会被处理成普通文本。当设置了参数runat=”template”的时候我们就可以在script标签中放置一些比较有用的帮助方法,方便我们的调用,在上面的下拉选择中,设置了这个属性,并且枚举的enum名称和属性Type的名称一致,就可以调用脚本中定义的枚举了。
上述的模板完成图为:

在一些 特殊的场合中会有标签<%重复的问题 ,例如在asp.net或是jsp等等场合中,那么我们可以先将asp.net或jsp中使用的这个重复标签写成这种形式<%%,在代码叶,标签重复引起的编译错误或生成错误。

常量的使用 是必不可少的,那么如何在codesmith中声明一个常量呢?代码很简单如下,和使用下拉列表的定义差不多。
<script runat="template">
private const string COUNT="example";
</script>


额外参数讲解
代码模板的声明(CodeTemplate Directive):
Inherits :所有CodeSmith模板默认继承自CodeSmith.Engine.CodeTemplate,这个
类提供模板使用的一些基本功能,像ASP.NET页面的Page类,这些被继承的类的属性可以被
修改,但是这些新的类也必须继承CodeSmith.Engine.CodeTemplate。CodeSmith也同样
可以找到这个类,当然你要引入一个组件包含这个类。
Src :在某些方面Src和继承Inherits比较相似,它们都允许你从其他的类包含一些功能进
模板。这两个属性的区别是,Src可以让类与你的模板被动态编译,而Inherits仅允许你提供
一个已经编译好的类或组件。
Debug :可以确定是否在模板中可以包含调试符号。如果将这个属性设置为True,则可以
使用System.Diagnostics.Debugger.Break()方法来设置断点。


属性的声明(Property Directive):
Optional :设置这个属性是否是必须的,设置为True则这个参数必须有值,设置为False则表明这个参数值可有可无。
Editor :表明在属性面板中输入这个属性的值时使用何种GUI(图形界面编辑器)编辑器。
EditorBase :编辑器使用的基本类型,如果没有被说明,UITypeEditor为默认编辑器。
Serializer :序列化器参数指定类型时要使用的属性值序列化


XML属性声明(XmlProperty Directive)
例:<%@ XmlProperty Name="EntityMap" Schema="EntityMap.xsd" Optional="False" Category="Context" Description="EntityMap XML file to base the output on." %>
XML属性的参数:
     Name: 名称。
     Schema: 这个参数用来指定一个XSD文件,创建一个强类型对象模型。如果这个计划被指定,编译器会尝试分析这个XSD文件并为这个计划生成一个强类型对象模型,这样可以在模版中使用强类型和智能与XML协同工作。如果这个计划没有被设定,这个参数将为XmlDocument类型并且将使用XML DOM去导航到一个XML内容并生成代码。
     Category :在CodeSmith属性面板中的类别。
     Description :描述。
     Optional :这个参数是否是必须的,如果设置为True,则参数不是必须的,反之False则为必须的。在设置为False时,如果用户没有提供参数则CodeSmith不能继续运行。
 
注册的声明(Register Directive)
    这个属性通常被用作引入另一个模版文件并与当前的模版文件同时被编译。这是一种使用子模版的交互方法。
    例:<%@ Register Name="MySubTemplate" Template="MySubTemplate.cst" MergeProperties="True" ExcludeProperties="SomeExcludedPropertyName,SomeProperties*" %>
    模版一旦被注册,就可以建立一个模版的实例,然后象这样设置它的属性:
 
[plain]  view plain copy print ?
  1. <script runat="template">  
  2.   public void OutputSubTemplate()  
  3.   {  
  4.      MySubTemplate mySubTemplate = new MySubTemplate();  
  5.     
  6.      // set an individual properties value.  
  7.      mySubTemplate.SomeExcludedPropertyName = "SomeValue";  
  8.       
  9.      // copy all properties with matching name and type to the sub template instance.  
  10.     this.CopyPropertiesTo(mySubTemplate);  
  11.    
  12.     // render the template to the current templates Response object.  
  13.     mySubTemplate.Render(this.Response);  
  14.    
  15.     // render the template to a file.  
  16.     mySubTemplate.RenderToFile("C:\SomeFile.txt");  
  17.  }  
  18.  </script>  

注册的参数:
    Name
:代表被引入的模版的名称。它可以被用作创建一个模版的实例。
     Template :被引入模版文件的相对路径,它可以与当前的模版一起被动态的编译。
     MergeProperties :设置成True时,所有被引用的面板的属性将被动态的添加到当前模版中。
     ExcludePorperties :当使用MergeProperties时,你可能不需要某些属性被添加到当前模版中。将不需要的属性以逗号分隔放在这里,*号可以被用作通配符使用。


组件的声明(Assembly Directive)
    用作在模版中引用一个外部部组件,或者包含一个编译好的源文件。
例:<%@ Assembly Name="SchemaExplorer" %>
或<%@ Assembly Src="MySourceFile.cs" %>
    CodeSmith自动加载一些不同的组件:System, System.Diagnostics, System.ComponentModel, Microsoft.VisualBasic, CodeSmith.Engine

组件的参数:
    Name
:需要引用组件的名称,组建必须存在于Global Assembly Cache,与CodeSmith在同一路径下或与模版文件在同一路径下。
     Src :要包含文件的相对路径。

引入的声明(Import Directive)
    在模版中引入一个命名空间,这个与VB.NET中的Imports和C#中的using相同。
    例:<%@ Import Namespace="SchemaExplorer" %>
引入的参数:
     NameSpace :被引入的命名空间的名字。记住同时必须要加载包含这个命名空间的相应组件,除非这个组件是被默认加载的。



使用FileNameEditor

FileNameEditor类给我们提供了在CodeSmith属性面板中弹出打开或保存文件对话框的方式,
在使用时,首先在模版中得添加对程序集CodeSmith.CustomProperties的引用。然后就
可以在模版中定义一个属性来使用FileNameEditor:
[plain]  view plain copy print ?
  1. <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="" %>  
  2. <%@ Assembly Name="CodeSmith.CustomProperties" %>  
  3. <%@ Import Namespace="CodeSmith.CustomProperties" %>  
  4. <script runat="template">  
  5.    private string _filename;  
  6.   [Editor(typeof(FileNameEditor),typeof(System.Drawing.Design.UITypeEditor)),FileDialogAttribute(FileDialogType.Save,Title="save as",Filter="C# File|(*.cs)")]  
  7.    public string TargetFileName  
  8.    {  
  9.     get  
  10.     {  
  11.      return _filename;  
  12.     }  
  13.     set  
  14.     {  
  15.      _filename = value;  
  16.     }  
  17.    }  
  18. </script>  

添加 CodeSmith.CustomProperties 程序集:<%@ Assembly Name="CodeSmith.CustomProperties" %>
导入命名空间:<%@ Import Namespace="CodeSmith.CustomProperties" %>
FileDialogType  对话框的类型:Save/Open
FileDialogAttribute (FileDialogType.Save,Title="save as",Filter="C# File|(*.cs)")]: FileDialogType.Save 为设置打开的文件对话框为保存的对话框,标题为save as,刷选文件的类型:C# File|(*.cs)

打开选择文件对话框:
[plain]  view plain copy print ?
  1. <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="" %>  
  2. <%@ Assembly Name="System.Design" %>  
  3. <script runat="template">  
  4.    private string _outputDirectory = @"c:\temp";  
  5.  [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System  
  6. .Drawing.Design.UITypeEditor)),  
  7. Category("Custom"), Description("Output directory.")]  
  8.   public string OutputDirectory  
  9. {  
  10.  get {return _outputDirectory;}  
  11.  set {_outputDirectory= value;}  
  12. }  
  13. </script>  

private string _outputDirectory  = @"c:\temp";设置显示的默认文件路径。
Description("Output directory.") :codeSmith属性显示的名称。
需要引入这个程序集:<%@ Assembly Name="System.Design" %>

文件对话框的其他属性
DefaultExtension :默认扩展,即扩展属性
CheckFileExists :检查文件是否存在,默认为false
CheckPathExists :检查路径是否存在,默认也为false

生成的代码输出到文件中:
在CodeSmith中,要把生成的代码文件输出到文件中,你需要在自己的模版中 继承OutputFileCodeTemplate类
代码如下:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="OutputFi
leCodeTemplate" Description="Build custom access code."%>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
OutputFileCodeTemplate主要做两件事情:
1.它添加一个名为OutputFile的属性到你的模版中,该属性要求你必须选择一个文件;
2.模版重载了方法OnPostRender(),在CodeSmith生成代码完成后把相应的内容写入到指定的文件中去。
如果想要自定义OutputFile属性弹出的保存文件对话框,你需要在你的模版中 重载OutputFile 属性。
例如:你希望用户选择一个.cs文件来保存生成的代码,需要在你的模版中添加如下代码:
[plain]  view plain copy print ?
  1. <script runat="template">  
  2. // Override the OutputFile property and assign our specific settings to it.  
  3. [FileDialog(FileDialogType.Save, Title="Select Output File", Filter="  
  4. C# Files (*.cs)|*.cs", DefaultExtension=".cs")]  
  5. publicoverride string OutputFile  
  6. {  
  7. get {return base.OutputFile;}  
  8. set {base.OutputFile = value;}  
  9. }  
  10. </script>  

使用Progress 对象
Progress对象可以在CodeSmith生成代码时给用户显示一个进度条,当生成代码的时间很长时
非常有用。如果你使用的是CodeSmith Explorer,进度条将显示在Generate按钮的左边:
使用Progress和在WinForm中使用进度条差不多,需要设置它的最大值和步长:
this.Progress.MaximumValue  =25;
this.Progress.Step  = 1;
如果想显示出进度,需要调用PerformStep方法:
this.Progress.PerformStep();

使用快捷键
在CodeSmith中,以下几个快捷键有助于我们快速输入。
1.Ctrl + Shift + C
在空行上,按下Ctrl + Shift + C后将会录入一个代码块。
<% %>

2.Ctrl + Shift + Q
按下Ctrl + Shift + Q后录入一个脚本块。
<script runat="template">
</script>

3.Ctrl + Shift + V
对代码块反转,如有下面这样一行代码:
<%for(int i=0;i<10;i++){}%>
在两个大括号之间按下Ctrl + Shift + V后,将变成如下代码:<%for(int i=0;i<10;i++){%> <%}%>

4.Ctrl + Shift + W
按下Ctrl + Shift + W后会录入一个输出的代码块:
<%= %>

注意:在使用快捷键的时候,如果想要把一段代码之间放在录入的标记中间,首先选中这些代码,

再按下快捷键组合。比如我们有一段这样的代码,想把它放在<script>里面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值