CodeSmith实用技巧(一):使用StringCollection

StringCollection提供了一种集合的输入方式,在代码中,可以用Array的方式来引用。在使用这个类之前,在模版中我们必须添加对CodeSmith.CustomProperties程序集的引用:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

CodeSmith实用技巧(一):使用StringCollection<%@ Assembly Name="CodeSmith.CustomProperties" %>

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

CodeSmith实用技巧(一):使用StringCollection<%@ Property Name="List" Type="CodeSmith.CustomProperties.StringCollection" Category="Custom" Description="This is a sample StringCollection" %>

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

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />CodeSmith实用技巧(一):使用StringCollection

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

CodeSmith实用技巧(一):使用StringCollection

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

模版代码如下:

 1CodeSmith实用技巧(一):使用StringCollection<%@ CodeTemplate Language="C#" TargetLanguage="C#" %>
 2CodeSmith实用技巧(一):使用StringCollection
 3CodeSmith实用技巧(一):使用StringCollection<%@ Assembly Name="CodeSmith.CustomProperties" %>
 4CodeSmith实用技巧(一):使用StringCollection
 5CodeSmith实用技巧(一):使用StringCollection<%@ Property Name="List" Type="CodeSmith.CustomProperties.StringCollection" Category="Custom" Description="This is a sample StringCollection" %>
 6CodeSmith实用技巧(一):使用StringCollection
 7CodeSmith实用技巧(一):使用StringCollectionusing System;
 8CodeSmith实用技巧(一):使用StringCollectionnamespace Test
 9CodeSmith实用技巧(一):使用StringCollection
10CodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollection{       
11CodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollection         /**//// <summary>
12CodeSmith实用技巧(一):使用StringCollection
13CodeSmith实用技巧(一):使用StringCollection         ///     测试StringCollection
14CodeSmith实用技巧(一):使用StringCollection
15CodeSmith实用技巧(一):使用StringCollection         /// </summary>

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

32CodeSmith实用技巧(一):使用StringCollection
33CodeSmith实用技巧(一):使用StringCollection         }

34CodeSmith实用技巧(一):使用StringCollection
35CodeSmith实用技巧(一):使用StringCollection}

36CodeSmith实用技巧(一):使用StringCollection
37CodeSmith实用技巧(一):使用StringCollection

生成后的代码:

 1CodeSmith实用技巧(一):使用StringCollectionusing System;
 2CodeSmith实用技巧(一):使用StringCollection
 3CodeSmith实用技巧(一):使用StringCollectionnamespace Test
 4CodeSmith实用技巧(一):使用StringCollection
 5CodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollection{       
 6CodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollection         /**//// <summary>
 7CodeSmith实用技巧(一):使用StringCollection
 8CodeSmith实用技巧(一):使用StringCollection         ///     测试StringCollection
 9CodeSmith实用技巧(一):使用StringCollection
10CodeSmith实用技巧(一):使用StringCollection         /// </summary>

11CodeSmith实用技巧(一):使用StringCollection
12CodeSmith实用技巧(一):使用StringCollection         public class Test
13CodeSmith实用技巧(一):使用StringCollection
14CodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollection         CodeSmith实用技巧(一):使用StringCollection{
15CodeSmith实用技巧(一):使用StringCollection
16CodeSmith实用技巧(一):使用StringCollection                   public static void Main(string[] args)
17CodeSmith实用技巧(一):使用StringCollection
18CodeSmith实用技巧(一):使用StringCollectionCodeSmith实用技巧(一):使用StringCollection                   CodeSmith实用技巧(一):使用StringCollection{
19CodeSmith实用技巧(一):使用StringCollection
20CodeSmith实用技巧(一):使用StringCollection                            Console.WriteLine(Apples);
21CodeSmith实用技巧(一):使用StringCollection
22CodeSmith实用技巧(一):使用StringCollection                            Console.WriteLine(Fish);
23CodeSmith实用技巧(一):使用StringCollection
24CodeSmith实用技巧(一):使用StringCollection                   }

25CodeSmith实用技巧(一):使用StringCollection
26CodeSmith实用技巧(一):使用StringCollection         }

27CodeSmith实用技巧(一):使用StringCollection
28CodeSmith实用技巧(一):使用StringCollection}

29CodeSmith实用技巧(一):使用StringCollection
  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 的指定索引处的字符串

 

转载于:https://www.cnblogs.com/wayne-ivan/archive/2007/06/15/784398.html