CodeSmith2-Property(属性)

属性简介

即生成代码需要的输入值变量

<%@ Property Name="NameSpace" Type="String" Category="Context" Description="The namespace to use for this class" %>

加了属性后点生成,在CodeSmith Explorer中选择模板后生成代码的窗口中,变量的名称为NameSpace,类型是String,类别是Context。

参数

  • Name:模版使用的参数的名称。
  • Type:参数类型可以是任何.NET有效的数据类型,例如简单的String类型或者是CodeSmith的SchemaExplorer.DatabaseSchema类型。注意,类型必须是基类库的类型,例如用String或者Int32代替string和int。
  • Default:设置默认值。
  • Category:用来说明这个属性在CodeSmith Explorer的属性面板中显示成什么类型,例如下拉选择、直接输入等。
  • Description:属性的描述。
  • Optional:设置这个属性是否是必须的,设置为True表明这个参数值可有可无,设置为False则这个参数必须有值。
  • Editor:表明在属性面板中输入这个属性的值时使用何种GUI(图形界面编辑器)编辑器。
  • EditorBase:编辑器使用的基本类型,如果没有被说明,UITypeEditor为默认编辑器。
  • Serializer:?

另一种方式声明属性

<script runat="template">
  [Browsable(true)]
  [DefaultValue("")]
  [Description("描述")]
  public string Prop1{get;set;}
</script>

案例

数据表类型的属性

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context"  Description="Table that the stored procedures should be based on." %>

<script runat="template">
public SchemaExplorer.TableSchema SourceTable { get; set; }
</script>

数据库类型的属性

    public DatabaseSchema SourceDatabase {get;set;}

StringCollection类型属性

StringCollection提供了一种集合的输入方式,在代码中,可以用Array的方式来引用。
使用这个类之前,添加对程序集的引用<%@ Assembly Name="CodeSmith.CustomProperties" %>
定义属性:

<%@ Property Name="List" Type="CodeSmith.CustomProperties.StringCollection" Category="Custom" Description="This is a sample StringCollection" %>

FileNameEditor类型属性

FileNameEditor类给我们提供了在CodeSmith属性面板中选择文件对话框的方式。
使用这个类之前,添加对程序集的引用<%@ Assembly Name="CodeSmith.CustomProperties" %>

[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),Category("Custom"), Description("User selected file.")]
public string UserFileName{get;set;}

FileDialogAttribute
使用FileDialogAttribute可以设置FileNameEditor中的属性,基本成员如下:

  • FileDialogType保存或打开
    默认值:FileDialogType.Save
  • Filter 过滤字符串
    默认值:All Files (.)|.
  • Title标题
  • DefaultExtension默认后缀,没有默认值
  • CheckFileExistsTrue只允许选择现有的文件
    默认值:false
  • CheckPathExistsTrue只允许使用现有路径
    默认值false
<script runat="template">
 private string _openFileName = @"c:\temp\test.txt";
 [Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
 FileDialogAttribute(FileDialogType.Open, Title="Select Input File"),
 Category("Custom"), Description("User selected file.")]
 public string OpenFileName
 {
       get {return _openFileName;}
       set {_openFileName= value;}
 }
</script>

Enum类型属性

创建一个可以下拉选择

<%@ Property Name="CollectionType" Type="CollectionTypeEnum" Category="Context" Description="Type of collection" %>
<script runat="template">
  public enum CollectionTypeEnum
  {
      Vector,
      HashTable,
      SortedList
 }
</script>

Editor:FolderNameEditor

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

<%@ Property Name="CollectionType2" Type="String" Editor="System.Windows.Forms.Design.FolderNameEditor" %>

<script runat="template">
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor),typeof(System.Drawing.Design.UITypeEditor))]
public string OutputDirectory{get;set;}
</script>

posted @ 2021-09-17 19:29  .Neterr  阅读(116)  评论(0编辑  收藏  举报