CodeSmith基础(一)

 
  请大家耐心看完所有的基础文章,前两篇网上发表的比较多,是 CodeSmith 英文帮助文档的第一篇,我后面写的基础是将其他所有的英文帮助全部翻译出来了,全部为本人手写翻译,希望对大家有所帮助 http://www.cnblogs.com/Emoticons/QQ/49.gif

       
创建好一个模板后第一步要指明这是一个 C# 语言的模板。
http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif < %@ CodeTemplate  Language ="C#"  TargetLanguage ="C#"
http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif      Description
="Generates a class including a special informational header"  % >

       
第二步,我们要指明模板在生成代码时的属性,即生成代码需要的输入值变量。
< %@ Property  Name ="NameSpace"  Type ="String"
      Category
="Context"
      Description
="The namespace to use for this class"  % >
        如上边所示,在进行代码生成时,在 CodeSmith Explorer 中选择模板后生成代码的窗口中,变量的名称为 NameSpace ,类型是 String ,类别是 Context ,当用户选中这个属性时对于属性的描述 Description
       
我们可以按照 C# 语言的语法去使用定义的变量,例如:
///
// 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"  % >
///
// 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 中双击这个模板就会看到相应的属性界面,这里的属性均是我们在前边定义的属性。
http://bear-study-hard.cnblogs.com/images/cnblogs_com/bear-study-hard/CodeSmith基础1.jpg
按下 Generate 按钮生成,即可实现一个简单的类代码的生成。
  1 http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif /**/ ///
 2http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif// File: MyClass.cs
 3http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif// Description: Enter summary here after generation.
 4http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif// ---------------------
 5http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif// Copyright © 2003 Our Client
 6http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif// ---------------------
 7http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif// History
 8http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif//    12/2/2003    Mr. Smith    Original Version
 9http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif/**////
10http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif 
11http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
12http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gif 
13http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/None.gifnamespace MyNameSpace
14http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gifhttp://www.cnblogs.com/Images/dot.gif{
15http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif      /**//// <summary>
16http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/InBlock.gif      /// Summary description for MyClass.
17http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif      /// </summary>
18http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/InBlock.gif      public class MyClass
19http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif      http://www.cnblogs.com/Images/dot.gif{
20http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/InBlock.gif            public MyClass()
21http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif            http://www.cnblogs.com/Images/dot.gif{
22http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/InBlock.gif                  //
23http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/InBlock.gif                  // TODO: Add constructor logic here
24http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/InBlock.gif                  //
25http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
26http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif      }
27http://bear-study-hard.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}

生成后的代码即可放入 Visual Studio .NET 中使用,我们使用 CodeSmith 的目的就是为了快速高效的开发。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值