|
Posted on
2004-11-10 11:45
被遗弃的小指
阅读( 1912)
评论()
编辑
收藏
举报
写程序的时候,写那些集合类真是麻烦,如果不自己写的话,通常我们会使用SharpDevelop或者CodeSmith来生成,他们生成代码的确比较好,但是相对来说还是比较麻烦些。 于是乎,就动手自己用CodeDOM写了一个程序,嗯哪,也算是练练手吧。 主要的代码如下:
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
using Microsoft.VJSharp;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace Twodays.Tools.CollectionMaker
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 该类用于生成一个强类型的集合类
/// </summary>
public sealed class StrongTypeCollection
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
private StrongTypeCollection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
}
![](/Images/OutliningIndicators/InBlock.gif)
const string COPYRIGHT_INFO="TWODAYS CollectionMaker";
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static CodeCompileUnit Build(string nameSpace,string itemClassName,string collectionClassName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CodeCompileUnit CompileUnit = new CodeCompileUnit();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 增加文件的整体信息#region 增加文件的整体信息
![](/Images/OutliningIndicators/InBlock.gif)
CodeNamespace codeNamespace = new CodeNamespace(nameSpace);
codeNamespace.Comments.Clear();
codeNamespace.Comments.Add(new CodeCommentStatement("-------------------------------------------------------------" ,false) );
codeNamespace.Comments.Add(new CodeCommentStatement("" ,false) );
codeNamespace.Comments.Add(new CodeCommentStatement(" Powered By: " + COPYRIGHT_INFO,false) );
codeNamespace.Comments.Add(new CodeCommentStatement(" Created By: " + System.Environment.UserName,false) );
codeNamespace.Comments.Add(new CodeCommentStatement(" Created Time: " + DateTime.Now.ToString(),false));
codeNamespace.Comments.Add(new CodeCommentStatement("" ,false) );
codeNamespace.Comments.Add(new CodeCommentStatement("-------------------------------------------------------------" ,false) );
codeNamespace.Imports.Add( new CodeNamespaceImport("System") );
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
CompileUnit.Namespaces.Add( codeNamespace );
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 增加集合类#region 增加集合类
![](/Images/OutliningIndicators/InBlock.gif)
CodeTypeDeclaration collectionClass = new CodeTypeDeclaration(collectionClassName);
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 增加集合类的信息#region 增加集合类的信息
collectionClass.BaseTypes.Add("CollectionBase");
collectionClass.Comments.Add(new CodeCommentStatement("<summary>",true));
collectionClass.Comments.Add(new CodeCommentStatement("<para>",true));
collectionClass.Comments.Add(new CodeCommentStatement("A collection that stores <see cref='"+itemClassName + "'/> objects.",true));
collectionClass.Comments.Add(new CodeCommentStatement("</para>",true));
collectionClass.Comments.Add(new CodeCommentStatement("</summary>",true));
collectionClass.Comments.Add(new CodeCommentStatement("<seealso cref='"+ collectionClassName +"'/>",true));
collectionClass.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable"));
codeNamespace.Types.Add(collectionClass);
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 子项目数组的引用#region 子项目数组的引用
![](/Images/OutliningIndicators/InBlock.gif)
CodeTypeReference itemArrayReference=new CodeTypeReference(itemClassName) ;
itemArrayReference.ArrayElementType =new CodeTypeReference(itemClassName) ;
itemArrayReference.ArrayRank=1;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 构造函数#region 构造函数
collectionClass.Members.Add(CreateConstructor(collectionClassName,"",""));
CodeConstructor cc1= CreateConstructor(collectionClassName," based on another <see cref='" + collectionClassName +"'/>","A <see cref='" + collectionClassName + "'/> from which the contents are copied");
cc1.Parameters.Add(new CodeParameterDeclarationExpression(collectionClassName,"val"));
CodeMethodInvokeExpression cc1_invoke=new CodeMethodInvokeExpression(new CodeThisReferenceExpression(),"AddRange",new CodeArgumentReferenceExpression("val"));
cc1.Statements.Add(cc1_invoke);
collectionClass.Members.Add(cc1);
CodeConstructor cc2=CreateConstructor(collectionClassName," containing any array of <see cref='"+itemClassName+"'/> objects","A array of <see cref='"+ itemClassName +"'/> objects with which to intialize the collection");
cc2.Parameters.Add(new CodeParameterDeclarationExpression(itemArrayReference,"val"));
CodeMethodInvokeExpression cc2_invoke=new CodeMethodInvokeExpression(new CodeThisReferenceExpression(),"AddRange",new CodeArgumentReferenceExpression("val"));
cc2.Statements.Add(cc2_invoke);
collectionClass.Members.Add(cc2);
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 索引器#region 索引器
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberProperty indexer = new CodeMemberProperty();
indexer.Attributes = MemberAttributes.Final | MemberAttributes.Public;
indexer.Name = "Item";
indexer.Type = new CodeTypeReference(itemClassName);
indexer.Parameters.Add(new CodeParameterDeclarationExpression(typeof (int), "index"));
indexer.HasGet = true;
indexer.HasSet = true;
indexer.GetStatements.Add(
new CodeMethodReturnStatement(
new CodeCastExpression(
itemClassName,
new CodeIndexerExpression(
new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(), "List"),
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new CodeExpression[] {new CodeArgumentReferenceExpression("index")}
)
)
)
);
indexer.SetStatements.Add(
new CodeAssignStatement(
new CodeIndexerExpression(
new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(), "List"),
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new CodeExpression[] {new CodeArgumentReferenceExpression("index")}
),
new CodeArgumentReferenceExpression("value")
)
);
indexer.Comments.Add(new CodeCommentStatement("<summary>",true));
indexer.Comments.Add(new CodeCommentStatement("<para>Represents the entry at the specified index of the <see cref='" +itemClassName + "'/>.</para>",true));
indexer.Comments.Add(new CodeCommentStatement("</summary>",true));
indexer.Comments.Add(new CodeCommentStatement("<param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>",true));
indexer.Comments.Add(new CodeCommentStatement("<val>",true));
indexer.Comments.Add(new CodeCommentStatement(" <para> The entry at the specified index of the collection.</para>",true));
indexer.Comments.Add(new CodeCommentStatement("</val>",true));
indexer.Comments.Add(new CodeCommentStatement("<exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>",true));
collectionClass.Members.Add(indexer);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Add方法#region Add方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod add=new CodeMemberMethod() ;
add.Attributes=MemberAttributes.Final |MemberAttributes.Public;
add.ReturnType=new CodeTypeReference(typeof(int)) ;
add.Name="Add";
add.Parameters.Add(new CodeParameterDeclarationExpression(itemClassName ,"val")) ;
add.Statements.Add(
new CodeMethodReturnStatement(
new CodeMethodInvokeExpression(
new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(),"List"),"Add",new CodeArgumentReferenceExpression("val")
)
)
);
add.Comments.Add(new CodeCommentStatement("<summary>",true));
add.Comments.Add(new CodeCommentStatement(" <para>Adds a <see cref='" + itemClassName + "'/> with the specified val to the",true));
add.Comments.Add(new CodeCommentStatement(" <see cref='" + collectionClassName + "'/> .</para>",true));
add.Comments.Add(new CodeCommentStatement("</summary>",true));
add.Comments.Add(new CodeCommentStatement("<param name='val'>The <see cref='" + itemClassName + "'/> to add.</param>",true));
add.Comments.Add(new CodeCommentStatement("<returns>",true));
add.Comments.Add(new CodeCommentStatement(" <para>The index at which the new element was inserted.</para>",true));
add.Comments.Add(new CodeCommentStatement("</returns>",true));
add.Comments.Add(new CodeCommentStatement("<seealso cref='" + collectionClassName + ".AddRange'/>",true));
collectionClass.Members.Add(add);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) AddRange方法#region AddRange方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod addrange1=new CodeMemberMethod() ;
addrange1.Attributes=MemberAttributes.Final |MemberAttributes.Public;
addrange1.Name="AddRange";
addrange1.Parameters.Add(new CodeParameterDeclarationExpression(itemArrayReference,"val")) ;
![](/Images/OutliningIndicators/InBlock.gif)
CodeIterationStatement forLoop ;
forLoop = CreateForLoop("Length");
addrange1.Statements.Add(new CodeVariableDeclarationStatement(typeof(int),"i") ) ;
addrange1.Statements.Add(forLoop);
addrange1.Comments.Add(new CodeCommentStatement("<summary>",true));
addrange1.Comments.Add(new CodeCommentStatement("<para>Copies the elements of an array to the end of the <see cref='" + collectionClassName + "'/>.</para>",true));
addrange1.Comments.Add(new CodeCommentStatement("</summary>",true));
addrange1.Comments.Add(new CodeCommentStatement("<param name='val'>",true));
addrange1.Comments.Add(new CodeCommentStatement(" An array of type <see cref='" + itemClassName + "'/> containing the objects to add to the collection.",true));
addrange1.Comments.Add(new CodeCommentStatement("</param>",true));
addrange1.Comments.Add(new CodeCommentStatement("<returns>",true));
addrange1.Comments.Add(new CodeCommentStatement(" <para>None.</para>",true));
addrange1.Comments.Add(new CodeCommentStatement("</returns>",true));
addrange1.Comments.Add(new CodeCommentStatement("<seealso cref='" + collectionClassName + ".Add'/>",true));
collectionClass.Members.Add(addrange1);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) AddRange方法#region AddRange方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod addrange2=new CodeMemberMethod() ;
addrange2.Attributes=MemberAttributes.Final |MemberAttributes.Public;
addrange2.Name="AddRange";
addrange2.Parameters.Add(new CodeParameterDeclarationExpression(collectionClassName,"val")) ;
addrange2.Statements.Add(new CodeVariableDeclarationStatement(typeof(int),"i") ) ;
forLoop=CreateForLoop("Count");
addrange2.Statements.Add(forLoop);
addrange2.Comments.Add(new CodeCommentStatement("<summary>",true));
addrange2.Comments.Add(new CodeCommentStatement(" <para>",true));
addrange2.Comments.Add(new CodeCommentStatement(" Adds the contents of another <see cref='" + collectionClassName + "'/> to the end of the collection.",true));
addrange2.Comments.Add(new CodeCommentStatement(" </para>",true));
addrange2.Comments.Add(new CodeCommentStatement("</summary>",true));
addrange2.Comments.Add(new CodeCommentStatement("<param name='val'>",true));
addrange2.Comments.Add(new CodeCommentStatement(" A <see cref='" + collectionClassName + "'/> containing the objects to add to the collection.",true));
addrange2.Comments.Add(new CodeCommentStatement("</param>",true));
addrange2.Comments.Add(new CodeCommentStatement("<returns>",true));
addrange2.Comments.Add(new CodeCommentStatement(" <para>None.</para>",true));
addrange2.Comments.Add(new CodeCommentStatement("</returns>",true));
addrange2.Comments.Add(new CodeCommentStatement("<seealso cref='" + collectionClassName + ".Add'/>",true));
collectionClass.Members.Add(addrange2);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Contains方法#region Contains方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod contains=new CodeMemberMethod() ;
contains.Name ="Contains";
contains.ReturnType=new CodeTypeReference(typeof(bool));
contains.Attributes=MemberAttributes.Final| MemberAttributes.Public;
contains.Parameters.Add(new CodeParameterDeclarationExpression(itemClassName,"val"));
contains.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(),"List"),"Contains",new CodeArgumentReferenceExpression("val"))));
contains.Comments.Add(new CodeCommentStatement("<summary>",true) );
contains.Comments.Add(new CodeCommentStatement("<para>Gets a val indicating whether the",true) );
contains.Comments.Add(new CodeCommentStatement(" <see cref='"+ collectionClassName + "'/> contains the specified <see cref='" + itemClassName + "'/>.</para>",true) );
contains.Comments.Add(new CodeCommentStatement("</summary>",true) );
contains.Comments.Add(new CodeCommentStatement("<param name='val'>The <see cref='" + itemClassName + "'/> to locate.</param>",true) );
contains.Comments.Add(new CodeCommentStatement("<returns>",true) );
contains.Comments.Add(new CodeCommentStatement("<para><see langword='true'/> if the <see cref='" + itemClassName + "'/> is contained in the collection; ",true) );
contains.Comments.Add(new CodeCommentStatement(" otherwise, <see langword='false'/>.</para>",true) );
contains.Comments.Add(new CodeCommentStatement("</returns>",true) );
contains.Comments.Add(new CodeCommentStatement("<seealso cref='" + collectionClassName+ ".IndexOf'/>",true) );
collectionClass.Members.Add(contains);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) CopyTo方法#region CopyTo方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod copyto=new CodeMemberMethod() ;
copyto.Name="CopyTo";
copyto.Attributes=MemberAttributes.Final | MemberAttributes.Public;
copyto.Parameters.Add(new CodeParameterDeclarationExpression(itemArrayReference,"array")) ;
copyto.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int),"index") ) ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) copyto.Statements.Add((new CodeMethodInvokeExpression(new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(),"List"),"CopyTo",new CodeExpression[] {new CodeArgumentReferenceExpression("array"),new CodeArgumentReferenceExpression("index") }) ) ) ;
copyto.Comments.Add(new CodeCommentStatement("<summary>",true));
copyto.Comments.Add(new CodeCommentStatement("<para>Copies the <see cref='" + collectionClassName + "'/> values to a one-dimensional <see cref='System.Array'/> instance at the ",true));
copyto.Comments.Add(new CodeCommentStatement(" specified index.</para>",true));
copyto.Comments.Add(new CodeCommentStatement("</summary>",true));
copyto.Comments.Add(new CodeCommentStatement("<param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='" + collectionClassName + "'/> .</para></param>",true));
copyto.Comments.Add(new CodeCommentStatement("<param name='index'>The index in <paramref name='array'/> where copying begins.</param>",true));
copyto.Comments.Add(new CodeCommentStatement("<returns>",true));
copyto.Comments.Add(new CodeCommentStatement(" <para>None.</para>",true));
copyto.Comments.Add(new CodeCommentStatement("</returns>",true));
copyto.Comments.Add(new CodeCommentStatement("<exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='"+ collectionClassName +"'/> is greater than the available space between <paramref name='arrayIndex'/> and the end of <paramref name='array'/>.</para></exception>",true));
copyto.Comments.Add(new CodeCommentStatement("<exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>",true));
copyto.Comments.Add(new CodeCommentStatement("<exception cref='System.ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>",true));
copyto.Comments.Add(new CodeCommentStatement("<seealso cref='System.Array'/>",true));
collectionClass.Members.Add(copyto);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) IndexOf方法#region IndexOf方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod indexof=new CodeMemberMethod() ;
indexof.Name ="IndexOf";
indexof.Attributes=MemberAttributes.Final| MemberAttributes.Public;
indexof.ReturnType =new CodeTypeReference(typeof(int)) ;
indexof.Parameters.Add(new CodeParameterDeclarationExpression(itemClassName,"val") ) ;
indexof.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(),"List"),"IndexOf",new CodeArgumentReferenceExpression("val")) ) );
indexof.Comments.Add(new CodeCommentStatement("<summary>",true));
indexof.Comments.Add(new CodeCommentStatement(" <para>Returns the index of a <see cref='" +itemClassName+ "'/> in ",true));
indexof.Comments.Add(new CodeCommentStatement(" the <see cref='" + collectionClassName + "'/> .</para>",true));
indexof.Comments.Add(new CodeCommentStatement("</summary>",true));
indexof.Comments.Add(new CodeCommentStatement("<param name='val'>The <see cref='" + itemClassName + "'/> to locate.</param>",true));
indexof.Comments.Add(new CodeCommentStatement("<returns>",true));
indexof.Comments.Add(new CodeCommentStatement("<para>The index of the <see cref='" + itemClassName + "'/> of <paramref name='val'/> in the ",true));
indexof.Comments.Add(new CodeCommentStatement("<see cref='" + collectionClassName + "'/>, if found; otherwise, -1.</para>",true));
indexof.Comments.Add(new CodeCommentStatement("</returns>",true));
indexof.Comments.Add(new CodeCommentStatement("<seealso cref='" + collectionClassName + ".Contains'/>",true));
collectionClass.Members.Add(indexof) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Insert方法#region Insert方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod insert=new CodeMemberMethod() ;
insert.Name ="Insert";
insert.Attributes =MemberAttributes.Final| MemberAttributes.Public;
insert.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int),"index") );
insert.Parameters.Add(new CodeParameterDeclarationExpression(itemClassName,"val") ) ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) insert.Statements.Add(new CodeMethodInvokeExpression(new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(),"List"),"Insert",new CodeExpression[] {new CodeArgumentReferenceExpression("index"),new CodeArgumentReferenceExpression("val")} ) );
insert.Comments.Add(new CodeCommentStatement("<summary>",true));
insert.Comments.Add(new CodeCommentStatement("<para>Inserts a <see cref='" + itemClassName + "'/> into the <see cref='" + collectionClassName + "'/> at the specified index.</para>",true));
insert.Comments.Add(new CodeCommentStatement("</summary>",true));
insert.Comments.Add(new CodeCommentStatement("<param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>",true));
insert.Comments.Add(new CodeCommentStatement("<param name=' val'>The <see cref='" + itemClassName + "'/> to insert.</param>",true));
insert.Comments.Add(new CodeCommentStatement("<returns><para>None.</para></returns>",true));
insert.Comments.Add(new CodeCommentStatement("<seealso cref='" + collectionClassName + ".Add'/>",true));
collectionClass.Members.Add(insert);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) GetEnumerator方法#region GetEnumerator方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod getenumerator=new CodeMemberMethod() ;
getenumerator.Name ="GetEnumerator";
getenumerator.Attributes =MemberAttributes.New | MemberAttributes.Public | MemberAttributes.Final;
getenumerator.ReturnType =new CodeTypeReference(itemClassName + "Enumerator") ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) getenumerator.Statements.Add(new CodeMethodReturnStatement(new CodeObjectCreateExpression(itemClassName + "Enumerator",new CodeExpression[] {new CodeThisReferenceExpression()} ) ) );
getenumerator.Comments.Add(new CodeCommentStatement("<summary>",true) ) ;
getenumerator.Comments.Add(new CodeCommentStatement(" <para>Returns an enumerator that can iterate through ",true) ) ;
getenumerator.Comments.Add(new CodeCommentStatement(" the <see cref='" + collectionClassName+ "'/> .</para>",true) ) ;
getenumerator.Comments.Add(new CodeCommentStatement("</summary>",true) ) ;
getenumerator.Comments.Add(new CodeCommentStatement("<returns><para>None.</para></returns>",true) ) ;
getenumerator.Comments.Add(new CodeCommentStatement("<seealso cref='System.Collections.IEnumerator'/>",true) ) ;
collectionClass.Members.Add(getenumerator) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Remove方法#region Remove方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod remove =new CodeMemberMethod() ;
remove.Name ="Remove";
remove.Attributes =MemberAttributes.Final| MemberAttributes.Public;
remove.Parameters.Add(new CodeParameterDeclarationExpression(itemClassName,"val") ) ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) remove.Statements.Add(new CodeMethodInvokeExpression(new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(),"List") ,"Remove",new CodeExpression[] {new CodeArgumentReferenceExpression("val") } ) ) ;
remove.Comments.Add(new CodeCommentStatement("<summary>",true) ) ;
remove.Comments.Add(new CodeCommentStatement(" <para> Removes a specific <see cref='"+ itemClassName +"'/> from the ",true) ) ;
remove.Comments.Add(new CodeCommentStatement(" <see cref='" + collectionClassName + "'/> .</para>",true) ) ;
remove.Comments.Add(new CodeCommentStatement("</summary>",true) ) ;
remove.Comments.Add(new CodeCommentStatement("<param name='val'>The <see cref='" +itemClassName+ "'/> to remove from the <see cref='" + collectionClassName +"'/> .</param>",true) ) ;
remove.Comments.Add(new CodeCommentStatement("<returns><para>None.</para></returns>",true) ) ;
remove.Comments.Add(new CodeCommentStatement("<exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>",true) ) ;
collectionClass.Members.Add(remove);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
collectionClass.Members.Add(GetEnumerator(itemClassName,collectionClassName));
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
return CompileUnit;
}
![](/Images/OutliningIndicators/InBlock.gif)
private static CodeTypeDeclaration GetEnumerator(string itemClassName,string collectionClassName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 枚举器类#region 枚举器类
![](/Images/OutliningIndicators/InBlock.gif)
CodeTypeDeclaration enumeratorClass = new CodeTypeDeclaration(itemClassName + "Enumerator");
enumeratorClass.BaseTypes.Add("IEnumerator");
enumeratorClass.Comments.Add(new CodeCommentStatement("<summary>",true) ) ;
enumeratorClass.Comments.Add(new CodeCommentStatement(" Enumerator of the <see cref='" +collectionClassName+ "'/>." ,true) ) ;
enumeratorClass.Comments.Add(new CodeCommentStatement("</summary>",true) ) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 私有变量#region 私有变量
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberField baseEnumerator=new CodeMemberField("IEnumerator","baseEnumerator") ;
CodeMemberField temp=new CodeMemberField("IEnumerable","temp") ;
enumeratorClass.Members.Add(baseEnumerator) ;
enumeratorClass.Members.Add(temp) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 构造函数#region 构造函数
![](/Images/OutliningIndicators/InBlock.gif)
CodeConstructor constructor=new CodeConstructor() ;
constructor.Attributes =MemberAttributes.Public;
constructor.Parameters.Add(new CodeParameterDeclarationExpression(collectionClassName,"mappings" ) ) ;
constructor.Statements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(),"temp"),new CodeCastExpression("IEnumerable",new CodeArgumentReferenceExpression("mappings"))) ) ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) constructor.Statements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),new CodeMethodInvokeExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"temp"),"GetEnumerator",new CodeExpression[] {} ) ) ) ;
constructor.Comments.Add(new CodeCommentStatement("<summary>",true) ) ;
constructor.Comments.Add(new CodeCommentStatement("The Constructor of" + enumeratorClass.Name,true) ) ;
constructor.Comments.Add(new CodeCommentStatement("</summary>",true) ) ;
constructor.Comments.Add(new CodeCommentStatement("<param name='mappings'> The <see cref='" + collectionClassName + "'/> containing the objects to initialize the collection.</param>",true) ) ;
enumeratorClass.Members.Add(constructor) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Current属性#region Current属性
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberProperty current= new CodeMemberProperty() ;
current.Type =new CodeTypeReference(itemClassName) ;
current.Name ="Current";
current.HasGet =true;
current.HasSet=false;
current.Attributes =MemberAttributes.Public | MemberAttributes.Final;
current.GetStatements.Add(new CodeMethodReturnStatement(new CodeCastExpression(itemClassName,new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),"Current") ) ) );
current.Comments.Add(new CodeCommentStatement("<summary>",true) ) ;
current.Comments.Add(new CodeCommentStatement("Get current <see cref='" + itemClassName + "'/>",true) ) ;
current.Comments.Add(new CodeCommentStatement("</summary>",true) ) ;
enumeratorClass.Members.Add(current);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 实现IEnumerator的Current属性#region 实现IEnumerator的Current属性
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberProperty iCurrent=new CodeMemberProperty() ;
iCurrent.Name ="IEnumerator.Current";
iCurrent.Type =new CodeTypeReference(typeof(object)) ;
iCurrent.Attributes=MemberAttributes.Final;
iCurrent.HasGet =true;
iCurrent.HasSet=false;
iCurrent.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator") ,"Current") ) ) ;
enumeratorClass.Members.Add(iCurrent) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) MoveNext方法#region MoveNext方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod movenext=new CodeMemberMethod() ;
movenext.Name ="MoveNext";
movenext.ReturnType =new CodeTypeReference(typeof(bool)) ;
movenext.Attributes =MemberAttributes.Final | MemberAttributes.Public ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) movenext.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),"MoveNext",new CodeExpression[] {} ) ));
movenext.Comments.Add(new CodeCommentStatement("<summary>",true) ) ;
movenext.Comments.Add(new CodeCommentStatement(" Set the current <see cref='" + itemClassName + "'/> to the next.",true) ) ;
movenext.Comments.Add(new CodeCommentStatement("</summary>",true) ) ;
movenext.Comments.Add(new CodeCommentStatement("<returns> If operate success return True else Flase.</returns>",true) ) ;
enumeratorClass.Members.Add(movenext) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 实现IEnumerator的MoveNext方法#region 实现IEnumerator的MoveNext方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod iMoveNext=new CodeMemberMethod() ;
iMoveNext.Name ="IEnumerator.MoveNext";
iMoveNext.ReturnType =new CodeTypeReference(typeof(bool)) ;
iMoveNext.Attributes =MemberAttributes.Final;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) iMoveNext.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),"MoveNext",new CodeExpression[] {} ) ));
enumeratorClass.Members.Add(iMoveNext) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Reset方法#region Reset方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod reset=new CodeMemberMethod() ;
reset.Name ="Reset";
reset.Attributes =MemberAttributes.Final | MemberAttributes.Public ;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) reset.Statements.Add(new CodeMethodInvokeExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),"Reset",new CodeExpression[] {} ) ) ;
reset.Comments.Add(new CodeCommentStatement("<summary>",true) ) ;
reset.Comments.Add(new CodeCommentStatement(" Reset current point of the IEnumerator.",true) ) ;
reset.Comments.Add(new CodeCommentStatement("</summary>",true) ) ;
enumeratorClass.Members.Add(reset) ;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) 实现IEnumerator的Reset方法#region 实现IEnumerator的Reset方法
![](/Images/OutliningIndicators/InBlock.gif)
CodeMemberMethod iReset=new CodeMemberMethod() ;
iReset.Name ="IEnumerator.Reset";
iReset.Attributes =MemberAttributes.Final;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) iReset.Statements.Add(new CodeMethodInvokeExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),"Reset",new CodeExpression[] {} ) ) ;
enumeratorClass.Members.Add(iReset) ;
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
return enumeratorClass;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static CodeIterationStatement CreateForLoop(string valPropertyName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CodeIterationStatement forLoop;
forLoop= new CodeIterationStatement(
new CodeAssignStatement( new CodeVariableReferenceExpression("i"), new CodePrimitiveExpression(0) ),
new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression("i"),
CodeBinaryOperatorType.LessThan, new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("val"),valPropertyName)),
new CodeAssignStatement( new CodeVariableReferenceExpression("i"), new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1) )),
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new CodeStatement[] { new CodeExpressionStatement( new CodeMethodInvokeExpression(new CodeThisReferenceExpression(),"Add",new CodeIndexerExpression(new CodeArgumentReferenceExpression("val"),new CodeVariableReferenceExpression("i")) ) ) });
return forLoop;
}
![](/Images/OutliningIndicators/InBlock.gif)
private static CodeConstructor CreateConstructor(string collectionClassName, string paraEnd,string paramInfo)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CodeConstructor cc =new CodeConstructor() ;
cc.Attributes =MemberAttributes.Public ;
cc.Comments.Add(new CodeCommentStatement("<summary>",true));
cc.Comments.Add(new CodeCommentStatement(" <para>",true));
cc.Comments.Add(new CodeCommentStatement(" Initializes a new instance of <see cref='" + collectionClassName + "'/>" + paraEnd + ".",true));
cc.Comments.Add(new CodeCommentStatement(" </para>",true));
cc.Comments.Add(new CodeCommentStatement("</summary>",true));
if(paramInfo.Length>0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cc.Comments.Add(new CodeCommentStatement("<param name='val'>",true));
cc.Comments.Add(new CodeCommentStatement(" "+paramInfo,true));
cc.Comments.Add(new CodeCommentStatement("</param>",true));
}
return cc;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 生成指定的类型的强类型集合
/// </summary>
/// <param name="itemTypeName">集合中的元素的类型</param>
/// <param name="nameSpace">代码所在的命名空间</param>
/// <param name="collectionClassName">集合类的名称</param>
/// <param name="codeType">代码的类型</param>
/// <param name="textWriter">指定的文本编写器</param>
public static void Make(string itemTypeName,string nameSpace,string collectionClassName,CodeType codeType,TextWriter textWriter)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CodeDomProvider provider;
switch(codeType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
case CodeType.CSharp:
provider=new CSharpCodeProvider();
break;
case CodeType.VB:
provider=new VBCodeProvider();
break;
case CodeType.JSharp:
provider=new VJSharpCodeProvider();
break;
default:
provider=new CSharpCodeProvider();
break;
}
ICodeGenerator gen=provider.CreateGenerator();
IndentedTextWriter tw = new IndentedTextWriter(textWriter, " ");
CodeGeneratorOptions cgo=new CodeGeneratorOptions() ;
cgo.BlankLinesBetweenMembers =true;
cgo.BracingStyle="C";
cgo.ElseOnClosing=true;
gen.GenerateCodeFromCompileUnit(Build(nameSpace,itemTypeName,collectionClassName), tw, cgo);
tw.Close();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 生成指定的类型的强类型集合
/// </summary>
/// <param name="itemTypeName">集合中的元素的类型</param>
/// <param name="nameSpace">代码所在的命名空间</param>
/// <param name="collectionClassName">集合类的名称</param>
/// <param name="codeType">代码的类型</param>
/// <param name="fileName">要生成的指定的文件名</param>
public static void Make(string itemTypeName,string nameSpace,string collectionClassName,CodeType codeType,string fileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
TextWriter tw= new StreamWriter(fileName, false);
Make(itemTypeName,nameSpace,collectionClassName,codeType,tw);
tw.Close();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 生成指定的类型的强类型集合
/// </summary>
/// <param name="itemTypeName">集合中的元素的类型</param>
/// <param name="nameSpace">代码所在的命名空间</param>
/// <param name="collectionClassName">集合类的名称</param>
/// <param name="codeType">代码的类型</param>
/// <returns>请类型集合的代码</returns>
public static string Make(string itemTypeName,string nameSpace,string collectionClassName,CodeType codeType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
string tempfile=System.IO.Path.GetTempFileName();
StrongTypeCollection.Make(itemTypeName ,nameSpace , collectionClassName ,codeType,tempfile) ;
string Result="";
using (StreamReader sr = new StreamReader(tempfile))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(int i=0;i<9;i++)
sr.ReadLine();
Result =sr.ReadToEnd();
}
System.IO.File.Delete(tempfile) ;
return Result;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 代码的类型
/// </summary>
public enum CodeType
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// C#
/// </summary>
CSharp,
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// VB.NET
/// </summary>
VB,
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// J#
/// </summary>
JSharp
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
}
完整代码下载地址: https://files.cnblogs.com/twodays/CollectionMaker.rar
编译好的程序下载地址: https://files.cnblogs.com/twodays/CollectionMaker.zip
|