CodeSmith应用实例(二)

三、微软的一个例子

       今天在微软的网站看到的一篇使用CodeSmith的例子,现在写出来大家一起研究研究。
        首先,我还是要简要介绍一下其中用到的基础知识。
        1.在模板中的代码区中(<%=  %>或<%  %>)可以使用.NET中的一些类和方法。但是就像和.NET项目中一样需要添加应用,就像C#中的 using        2.在脚本区域中可以编写生成模板时使用到的函数,其中的语言根据在声明模板时定义的使用的语言相同。
.个人感觉在编写模板时就像以前编写ASP页面一样,很灵活。其中的循环和判断的使用结构上和ASP基本一样,只不过在代码区域中使用的语言是声明模板时定义的语言,像C#、VB.NET等。

        这次我们要实现的一个功能是将某一文件夹下所有匹配后缀的文件全部找出并输出它们的绝对路径。其实这个模板本身的功能并不强大,主要是大家可以利用这个模板在生成代码时批量向代码文件中生成代码,例如向某一文件夹下的所有.cs文件中插入相应的代码。这个思想是基于一定条件的:①首先要学习我前边在CodeSmith基础中提到的,可以向代码文件中定义的region中添加相应的代码,②文件夹下的.cs文件需要有一定的命名规范,③.cs代码文件中的region区域的定义的名称也需要有一定的命名规范,②③这两个要求是结合起来的,因为我们得到同一文件夹下的所有.cs文件后,根据文件名去确定这个代码文件是哪一层的那个类文件,通过这个名称我们就知道需要读出那个数据表的结构,然后按照这个数据表去像相应命名好的region中添加相应的代码。主要思想就是这样的。下面将模板文件的代码贴出来大家研究。
< %@ Assembly  Name ="System.Data"  % >
< %@ Import  Namespace ="System. Data"  % >

< script  runat ="template" >

</ script >
        3
 1 <% @ CodeTemplate Language = " C# "   TargetLanguage = " Text "  Description = " Simple template to show main syntax "  %>
 2 <% @ Property Name = " Filter "  Default = " *.cst "  Type = " System.string "  Category = " Masks "  Description = " Mask for files in the directory "  %>
 3 <% @ Assembly Name = " SchemaExplorer "  %>
 4 <% @ Assembly Name = " System.Design "  %>
 5 <% @ Import Namespace = " SchemaExplorer "  %>
 6 <% @ Import Namespace = " System.IO "  %>
 7 Simple Template Example used to show syntax and structure of template. 
 8
 9 <%=  DateTime.Now.ToLongDateString()  %>
10
11 <%  
12 //  Comments within code delimiters or script blocks 
13 //  http://www.livebaby.cn ( C#)
14 Response.WriteLine( " List of files in template directory (using mask  " +  Filter  +  " ) " ); 
15 DisplayDirectoryContents(Filter);
16 Response.WriteLine( " >> Code Generation Complete. " );
17 %>
18
19 < script runat = " template " >
20 //  Iterates through the current directory and displays
21 //  a list of the files that conform to the supplied 
22 //  mask. 
23 public  void  DisplayDirectoryContents( string  sFilter)
24 {
25    string[] dirFiles = Directory.GetFiles
26       (this.CodeTemplateInfo.DirectoryName, sFilter);
27    for (int i = 0; i < dirFiles.Length; i++)
28    {
29         Response.WriteLine(dirFiles[i]);
30    }

31}
  
32 </ script >
 
 
四、实现选择路径对话框

首先我们要添加 <%@ Assembly Name="System.Design" %> 命名空间。然后我们在模板中自定义一个属性,用来表示要存储的路径。其中我们使用了 this.CodeTemplateInfo.DirectoryName 得到当前模版所在路径作为默认路径。
private  string  _outputDirectory  =  String.Empty;

[Editor(
typeof (System.Windows.Forms.Design.FolderNameEditor),  typeof (System.Drawing.Design.UITypeEditor))] 
[Optional]
[Category(
" Output " )]
[Description(
" The directory to output the results to. " )]
public  string  OutputDirectory 
{
         
get
         {
                   
//  default to the directory that the template is located in
                    if  (_outputDirectory.Length  ==  0 return  this .CodeTemplateInfo.DirectoryName  +  " output// " ;

                   
return  _outputDirectory;
         }
         
set
         {
                   
if  ( ! value.EndsWith( " // " )) value  +=  " // " ;
                   _outputDirectory 
=  value;
         } 
}
这样编译运行后我们就可以看到如下效果:

    单击选择路径按钮后我们就可以看到这样的窗口

    选择后相应的路径值就会填入属性框。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值