CodeSmith功能和技巧收集

官方网站:http://www.codesmithtools.com/

5.0破解文件下载:http://kewlshare.com/dl/0538fcf454d3/CodeSmith.5.0.Professional.Incl.Patch.DTCG.rar.html

资料:

http://blog.sina.com.cn/s/articlelist_1229294631_3_1.html

http://www.cnblogs.com/Terrylee/category/44974.html

快捷键:

1.Ctrl + Shift + C

在空行上,按下Ctrl + Shift + C后将会录入一个代码块。<% %>

2.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>里面。

public enum CollectionTypeEnum

{

 Vector, HashTable, SortedList

}

选中它,再按下Ctrl + Shift + Q后就会变成:

技巧集:

1.如何在模板中添加注释

CodeSmith:<%-- Comments --%>

VB.NET:<%-- 'Comments --%>

C#:<%-- // Comments --%> <%-- /* Comments */ --%>

2.创建一个可以下拉选择的属性

首先定义一个枚举类型的变量,然后将属性的类型设置为枚举型

<%@ Property Name="CollectionType" Type="CollectionTypeEnum" Category="Collection" Description="Type of collection" %>

<script runat="tempate">

public enum CollectionTypeEnum

{

      Vector,

      HashTable,

      SortedList

}

</script>

3.解决ASP.NET中标签<%重复问题

先将ASP.NET中使用的这个重复标签写成<%%,避免在生成代码时由于是标签重复引起的编译错误或生成错误。

4.如何声明一个常量 

<script runat="template">

private const string MY_CONST = "example";

</script>

5.如何对模板进行调试

如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用Debugger.Break()语句设置断点即可。

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Debugging your template" Debug="true" %>

<% Debugger.Break(); %>

6.如何将属性设置成选择一个文件夹的路径

[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]

public string OutputDirectory

{

      get {return _outputDirectory;}

      set {_outputDirectory= value;}

}

7.怎样调用子模板

<%

foreach (TableSchema table in SourceDatabase.Tables)

{

   OutputSubTemplate(table);

}

%>

<script runat="template">

private CodeTemplate _mySubTemplate;

Browsable(false)]

public CodeTemplate MySubTemplate

{

   get

   {

      if (_mySubTemplate == null)

      {

         CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName + "MySubTemplate.cst");

         compiler.Compile();

         if (compiler.Errors.Count == 0)

         {

            _mySubTemplate = compiler.CreateInstance();

         }

         else

         {

            for (int i = 0; i < compiler.Errors.Count; i++)

            {

               Response.WriteLine(compiler.Errors[ i].ToString());

            }

         }

      }

      return _mySubTemplate;

   }

}

public void OutputSubTemplate(TableSchema table)

{

   MySubTemplate.SetProperty("SourceTable", table);

   MySubTemplate.SetProperty("IncludeDrop", false);

   MySubTemplate.SetProperty("InsertPrefix", "Insert");

  MySubTemplate.Render(Response);

}

</script>

     

8.在加载模板时默认加载的命名空间Namespaces和组件Assemblies

组件:mscorlib, System, System.Xml, System.Data, System.Drawing, Microsoft.VisualBasic, System.Windows.Forms, CodeSmith.Engine

命名空间:System, System.Data, System.Diagnostics, System.ComponentModel, Microsoft.VisualBasic, CodeSmith.Engine

9.使用SchemaExplorer能否确定一个字段(Field)是标识字段(主键,Identity Field)

在字段的扩展属性集合中包含一个叫“CS_IsIdentity”的属性,如果这个属性的值为true,则表名当前字段为一个标识字段

Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {

       if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) == true)

       {

             Response.Write(cs.Name);

       }

 }

 %>

10.如何确定一个字段的默认值

在字段的扩展属性集合中包含一个叫“CS_Default”的属性

 <%

 foreach(ColumnSchema cs in SourceTable.Columns) {

       if (cs.ExtendedProperties["CS_Default"] != null)

       {

            Response.WriteLine(cs.ExtendedProperties["CS_Default"].Value);

       }

 }

 %>

11.如何使用SchemaExplorer得到存储过程的输入输出参数

使用CodeSmith提供的CommandSchema对象,它包含需要的输入输出参数集合

 Input Parameters:

 <%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)

 {

       Response.Write(ps.Name);

       Response.Write(""n");

 }

 %>

 Output Parameters:

 <%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)

 {

       Response.Write(ps.Name);

       Response.Write(""n");

 }

 %>

12.通过编程执行模版 CodeSmith在执行模版时通过调用一些API来完成的

主要经过了以下这几步的操作:

编译一个模版

显示编译错误信息

创建一个新的模版实例

用元数据填充模版

输出结果

下面这段代码显示了这些操作:

CodeTemplateCompiler compiler = new CodeTemplateCompiler("..""..""StoredProcedures.cst");

compiler.Compile();

if (compiler.Errors.Count == 0)

{

    CodeTemplate template = compiler.CreateInstance();

    DatabaseSchema database = new DatabaseSchema(new SqlSchemaProvider(), @"Server=(local)"NetSDK;Database=Northwind;Integrated Security=true;");

    TableSchema table = database.Tables["Customers"];

    template.SetProperty("SourceTable", table);

    template.SetProperty("IncludeDrop", false);

    template.SetProperty("InsertPrefix", "Insert");

    template.Render(Console.Out);

}

else

{

    for (int i = 0; i < compiler.Errors.Count; i++)

    {

        Console.Error.WriteLine(compiler.Errors[i].ToString());

    }

}

在这里我们用了Render方法,其实CodeTemplate.RenderToFile和CodeTemplate.RenderToString方法可能更有用,它可以直接让结果输出到文件中或赋给字符型的变量。

13.重载Render方法来控制输出

 在CodeSmith中,CodeTemplate.Render方法是在模版执行完成进行模版输出时执行,你可以通过重载CodeTemplate.Render方法来修改CodeSmith输出时的事件处理。例如:你可以修改模版输出时的方式来代替现在默认的方式,下面这段代码展示了在保持CodeSmith默认的窗口显示的同时,把结果输出到两个不同的文件。

 <%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="AddTextWriter Demonstration." %>

 <%@ Import Namespace="System.IO" %>

 This template demonstrates using the AddTextWriter method

 to output the template results to multiple locations concurrently.

<script runat="template">

 public override void Render(TextWriter writer)

 {

     StreamWriter fileWriter1 = new StreamWriter(@"C:"test1.txt", true);

     this.Response.AddTextWriter(fileWriter1);

     StreamWriter fileWriter2 = new StreamWriter(@"C:"test2.txt", true);

     this.Response.AddTextWriter(fileWriter2);

     base.Render(writer);

     fileWriter1.Close();

     fileWriter2.Close();

}

</script>注意不能忘了base.Render(writer);这句话,否则你将不能获得默认的输出。当重载CodeTemplate.Render方法时,你也可以访问TextWriter,也就是说你也可以直接添加其它的附属信息到模版输出的内容中。

14.生成的代码输出到文件中

在CodeSmith中,要把生成的代码文件输出到文件中,你需要在自己的模版中继承OutputFileCodeTemplate类。

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="OutputFileCodeTemplate" Description="Build custom access code." %>

<%@ Assembly Name="CodeSmith.BaseTemplates" %>

OutputFileCodeTemplate主要做两件事情:

1.它添加一个名为OutputFile的属性到你的模版中,该属性要求你必须选择一个文件;

2.模版重载了方法OnPostRender(),在CodeSmith生成代码完成后把相应的内容写入到指定的文件中去。

如果想要自定义OutputFile属性弹出的保存文件对话框,你需要在你的模版中重载OutputFile属性。例如:你希望用户选择一个.cs文件来保存生成的代码,需要在你的模版中添加如下代码:

<script runat="template">

// Override the OutputFile property and assign our specific settings to it.

[FileDialog(FileDialogType.Save, Title="Select Output File", Filter="C# Files (*.cs)|*.cs", DefaultExtension=".cs")]

public override string OutputFile

{

    get {return base.OutputFile;}

    set {base.OutputFile = value;}

}

</script>

15.从父模版拷贝属性

 在使用CodeSmith进行代码生成的时候,你可能需要在子模版和父模版之间共享属性。比如,写一个基于数据库生成代码的模版,在每个模版里面都定义了一个名为Server的属性。当你在父模版中使用此属性时,它的值只对父模版起作用。想要设置此值到子模版,可以在父模版中使用CopyPropertiesTo方法,当在父模版中使用此属性时,它的值会发送到子模版中去。下面这段代码展示了如何使用该方法:

 // instantiate the sub-template

   Header header = new Header();

   // copy all properties with matching name and type to the sub-template instance

   this.CopyPropertiesTo(header);

16.利用继承生成可变化的代码

 用CodeSmith生成可变化的代码,其实是先利用CodeSmith生成一个基类,然后自定义其它类继承于该类。当我们重新生成基类时CodeSmith不要接触继承的子类中的代码。看下面的这段模版脚本:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Base class generator." %>

<%@ Property Name="ClassName" Type="System.String" Description="Name of the class." %>

<%@ Property Name="ConstructorParameterName" Type="System.String" Description="Constructor parameter name." %>

<%@ Property Name="ConstructorParameterType" Type="System.String" Description="Data type of the constructor parameter." %>

class <%= ClassName %>

{

    <%= ConstructorParameterType %> m_<%= ConstructorParameterName %>;

    public <%= ClassName %>(<%= ConstructorParameterType %> <%= ConstructorParameterName %>)

    {

        m_<%= ConstructorParameterName %> = <%= ConstructorParameterName %>

    }

}

该模版生成的代码可能如下:

 class Account

 {

     int m_balance;

     public Account(int balance)

     {

         m_balance = balance

     }

}

把生成的文件保存为Account.cs文件。这时我们可以编写第二个类生成Check.cs文件代码:

class Checking : Account

{

    public Checking : base(0)

    {

    }

}

现在如果需要改变Account Balance的类型为浮点型,我们只需要改变ConstructorParameterType属性为float,并重新生成Account.cs文件即可而不需要直接在Account.cs中进行手工修改,并且不需要修改Check.cs文件的任何代码。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhaili1978/archive/2008/10/28/3167547.aspx

原文地址:https://www.cnblogs.com/luluping/p/1523969.html