zoukankan      html  css  js  c++  java
  • CodeSmith实用技巧(一):使用StringCollection

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

    添加完程序集之后,我们就可以使用StringCollection在脚本块中定义一个属性:

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

    执行该模版时,这个属性将在属性窗体中显示为一个按钮:

    单击按钮,将会弹出一个String Collection Editor对话框:

    当然也可以直接在属性窗口中编辑StringCollection

    模版代码如下:

     1<%@ CodeTemplate Language="C#" TargetLanguage="C#" %>
     2
     3<%@ Assembly Name="CodeSmith.CustomProperties" %>
     4
     5<%@ Property Name="List" Type="CodeSmith.CustomProperties.StringCollection" Category="Custom" Description="This is a sample StringCollection" %>
     6
     7using System;
     8namespace Test
     9
    10{       
    11         /// <summary>
    12
    13         ///     测试StringCollection
    14
    15         /// </summary>

    16
    17         public class Test
    18
    19         {
    20
    21                   public static void Main(string[] args)
    22
    23                   {
    24
    25                            <%for(int i = 0;i<List.Count;i++){%>
    26
    27                            Console.WriteLine(<%=List[i]%>);
    28
    29                            <%}
    %>
    30
    31                   }

    32
    33         }

    34
    35}

    36
    37

    生成后的代码:

     1using System;
     2
     3namespace Test
     4
     5{       
     6         /// <summary>
     7
     8         ///     测试StringCollection
     9
    10         /// </summary>

    11
    12         public class Test
    13
    14         {
    15
    16                   public static void Main(string[] args)
    17
    18                   {
    19
    20                            Console.WriteLine(Apples);
    21
    22                            Console.WriteLine(Fish);
    23
    24                   }

    25
    26         }

    27
    28}

    29
      StringCollection的重要属性和方法:

    公共属性

    名称

    描述

    Count

    获取StringCollection中包含的字符串的数目

    IsReadOnly

    获取用于指示StringCollection是否为只读的值

    IsSynchronized

    获取一个值,该值指示对StringCollection 的访问是否为同步的(线程安全的)

    Item

    获取或设置指定索引处的元素。在C# 中,该属性为 StringCollection 类的索引器

    SyncRoot

    获取可用于同步对StringCollection 的访问的对象

    公共方法

    名称

    描述

    Add

    将字符串添加到 StringCollection 的末尾

    AddRange

    将字符串数组的元素复制到 StringCollection 的末尾

    Clear

    移除 StringCollection 中的所有字符串

    Contains

    确定指定的字符串是否在 StringCollection

    CopyTo

    从目标数组的指定索引处开始,将全部 StringCollection 值复制到一维字符串数组中

    IndexOf

    搜索指定的字符串并返回 StringCollection 内的第一个匹配项的从零开始的索引

    Insert

    将字符串插入 StringCollection 中的指定索引处

    Remove

    StringCollection 中移除特定字符串的第一个匹配项

    RemoveAt

    移除 StringCollection 的指定索引处的字符串

     

    支持TerryLee的创业产品Worktile
    Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
    https://worktile.com
  • 相关阅读:
    Java :BufferedWriter类和BufferedReader类的构造方法、主要方法
    多线程:四大线程池详解
    多线程:head first Thread.join()
    多线程: 多线程中断机制
    多线程:深入Thread.sleep
    多线程:线程池原理
    java:java构造器和java方法的区别
    java: Java中this和super的用法总结
    postman: 用于网页调试和发送Http请求的chrome插件
    json:JSONObject包的具体使用(JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包)
  • 原文地址:https://www.cnblogs.com/Terrylee/p/304865.html
Copyright © 2011-2022 走看看