AppleStar

  • Increase font size
  • Default font size
  • Decrease font size
首页 Developer DotNet CodeSimth技巧

CodeSimth技巧

E-mail 打印 PDF
 1.如何在模板中添加注释
        CodeSmith:
        <%-- Comments --%>
        VB.NET:
        <%-- 'Comments --%>
        C#:
        <%-- // Comments --%>
        <%-- /* Comments */ --%>

        2.创建一个可以下拉选择的属性
        首先定义一个枚举类型的变量,然后将属性的类型设置为枚举型
 1 <%@ Property Name="CollectionType" Type="CollectionTypeEnum" Category="Collection" Deion="Type of collection" %>
 2 
 3 < runat="tempate">
 4 public enum CollectionTypeEnum
 5 {
 6     Vector,
 7     HashTable,
 8     SortedList
 9 }
10 </>

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

        4.如何声明一个常量
       
< runat="template">
private const string MY_CONST 
= "example"
</>

        5.如何对模板进行调试
        如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用Debugger.Break()语句设置断点即可。
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Deion="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= ;}
}
7.在加载模板时默认加载的命名空间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

        8.使用SchemaExplorer能否确定一个字段(Field)是标识字段(主键,Identity Field)
        在字段的扩展属性集合中包含一个叫“CS_IsIdentity”的属性,如果这个属性的值为true,则表名当前字段为一个标识字段
1 Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {  
2       if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].) == true)
3       {
4             Response.Write(cs.Name);
5       }
6 }
7 %>
        CS_Identity_Example.cst文件源代码
 1 <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
 2     Deion="Identifies the identity field of a table" %>
 3 
 4 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
 5     Category="Context"
 6     Deion="Table to target." %>
 7 
 8 <%@ Assembly Name="SchemaExplorer" %>
 9 
10 <%@ Import Namespace="SchemaExplorer" %>
11 
12 
13 
14 Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {  
15                         if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].) == true)
16                         {
17                             Response.Write(cs.Name);
18                         }
19                     }
20                  %>

        9.如何确定一个字段的默认值(各人认为翻译成如何知道一个字段有默认值并且默认值是什么)
        在字段的扩展属性集合中包含一个叫“CS_Default”的属性

1 <%
2 foreach(ColumnSchema cs in SourceTable.Columns) {  
3       if (cs.ExtendedProperties["CS_Default"!= null)
4       {
5            Response.WriteLine(cs.ExtendedProperties["CS_Default"].);
6       }
7 }
8 %>

        10.如何使用SchemaExplorer得到存储过程的输入输出参数
        使用CodeSmith提供的CommandSchema对象,它包含需要的输入输出参数集合
 1 Input Parameters: 
 2 <%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
 3 {
 4       Response.Write(ps.Name);
 5       Response.Write("\n");
 6 }
 7 %>
 8 
 9 
10 Output Parameters: 
11 <%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
12 {
13       Response.Write(ps.Name);
14       Response.Write("\n");
15 }
16 %>
        InputOutputParameterExample.cst文件源代码
 1 <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
 2     Deion="Generates a update stored procedure." %>
 3 
 4 <%@ Property Name="SourceProcedure" Type="SchemaExplorer.CommandSchema"
 5     Category="Context"
 6     Deion="The stored procedure to examine" %>
 7 
 8 <%@ Assembly Name="SchemaExplorer" %>
 9 
10 <%@ Import Namespace="SchemaExplorer" %>
11 
12 Input Parameters: 
13 <%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
14 {
15     Response.Write(ps.Name);
16     Response.Write("\n");
17 }
18 %>
19 
20 
21 Output Parameters: 
22 <%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
23 {
24     Response.Write(ps.Name);
25     Response.Write("\n");
26 }
27 %>

       
最后更新于: 2006-11-07 11:55  

添加评论


验证码
刷新

用户登录

ADS