|
|
Posted on
2004-11-10 11:45
被遗弃的小指
阅读( 1919)
评论()
收藏
举报
写程序的时候,写那些集合类真是麻烦,如果不自己写的话,通常我们会使用SharpDevelop或者CodeSmith来生成,他们生成代码的确比较好,但是相对来说还是比较麻烦些。 于是乎,就动手自己用CodeDOM写了一个程序,嗯哪,也算是练练手吧。 主要的代码如下:
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
using Microsoft.VJSharp;


namespace Twodays.Tools.CollectionMaker
  {
 /**//// <summary>
/// 该类用于生成一个强类型的集合类
/// </summary>
public sealed class StrongTypeCollection
 {
private StrongTypeCollection()
 {
}

const string COPYRIGHT_INFO="TWODAYS CollectionMaker";




private static CodeCompileUnit Build(string nameSpace,string itemClassName,string collectionClassName)
 {
CodeCompileUnit CompileUnit = new CodeCompileUnit();


 增加文件的整体信息#region 增加文件的整体信息

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 );

#endregion

 增加集合类#region 增加集合类

CodeTypeDeclaration collectionClass = new CodeTypeDeclaration(collectionClassName);
 增加集合类的信息#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

 子项目数组的引用#region 子项目数组的引用

CodeTypeReference itemArrayReference=new CodeTypeReference(itemClassName) ;
itemArrayReference.ArrayElementType =new CodeTypeReference(itemClassName) ;
itemArrayReference.ArrayRank=1;

#endregion

 构造函数#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

 索引器#region 索引器

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"),
 new CodeExpression[] {new CodeArgumentReferenceExpression("index")}
)
)
)
);
indexer.SetStatements.Add(
new CodeAssignStatement(
new CodeIndexerExpression(
new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(), "List"),
 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);

#endregion

 Add方法#region Add方法

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);

#endregion

 AddRange方法#region AddRange方法

CodeMemberMethod addrange1=new CodeMemberMethod() ;
addrange1.Attributes=MemberAttributes.Final |MemberAttributes.Public;
addrange1.Name="AddRange";
addrange1.Parameters.Add(new CodeParameterDeclarationExpression(itemArrayReference,"val")) ;

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);

#endregion

 AddRange方法#region AddRange方法

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);

#endregion

 Contains方法#region Contains方法

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);

#endregion

 CopyTo方法#region CopyTo方法

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") ) ;
 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);

#endregion

 IndexOf方法#region IndexOf方法

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) ;

#endregion

 Insert方法#region Insert方法

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") ) ;
 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);

#endregion

 GetEnumerator方法#region GetEnumerator方法

CodeMemberMethod getenumerator=new CodeMemberMethod() ;
getenumerator.Name ="GetEnumerator";
getenumerator.Attributes =MemberAttributes.New | MemberAttributes.Public | MemberAttributes.Final;
getenumerator.ReturnType =new CodeTypeReference(itemClassName + "Enumerator") ;
 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) ;

#endregion

 Remove方法#region Remove方法

CodeMemberMethod remove =new CodeMemberMethod() ;
remove.Name ="Remove";
remove.Attributes =MemberAttributes.Final| MemberAttributes.Public;
remove.Parameters.Add(new CodeParameterDeclarationExpression(itemClassName,"val") ) ;
 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);

#endregion

collectionClass.Members.Add(GetEnumerator(itemClassName,collectionClassName));

#endregion


return CompileUnit;
}

private static CodeTypeDeclaration GetEnumerator(string itemClassName,string collectionClassName)
 {
 枚举器类#region 枚举器类

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) ) ;

#endregion

 私有变量#region 私有变量

CodeMemberField baseEnumerator=new CodeMemberField("IEnumerator","baseEnumerator") ;
CodeMemberField temp=new CodeMemberField("IEnumerable","temp") ;
enumeratorClass.Members.Add(baseEnumerator) ;
enumeratorClass.Members.Add(temp) ;

#endregion

 构造函数#region 构造函数

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"))) ) ;
 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) ;

#endregion

 Current属性#region Current属性

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);

#endregion

 实现IEnumerator的Current属性#region 实现IEnumerator的Current属性

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) ;

#endregion

 MoveNext方法#region MoveNext方法

CodeMemberMethod movenext=new CodeMemberMethod() ;
movenext.Name ="MoveNext";
movenext.ReturnType =new CodeTypeReference(typeof(bool)) ;
movenext.Attributes =MemberAttributes.Final | MemberAttributes.Public ;
 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) ;

#endregion

 实现IEnumerator的MoveNext方法#region 实现IEnumerator的MoveNext方法

CodeMemberMethod iMoveNext=new CodeMemberMethod() ;
iMoveNext.Name ="IEnumerator.MoveNext";
iMoveNext.ReturnType =new CodeTypeReference(typeof(bool)) ;
iMoveNext.Attributes =MemberAttributes.Final;
 iMoveNext.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),"MoveNext",new CodeExpression[] {} ) ));
enumeratorClass.Members.Add(iMoveNext) ;

#endregion

 Reset方法#region Reset方法

CodeMemberMethod reset=new CodeMemberMethod() ;
reset.Name ="Reset";
reset.Attributes =MemberAttributes.Final | MemberAttributes.Public ;
 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) ;

#endregion

 实现IEnumerator的Reset方法#region 实现IEnumerator的Reset方法

CodeMemberMethod iReset=new CodeMemberMethod() ;
iReset.Name ="IEnumerator.Reset";
iReset.Attributes =MemberAttributes.Final;
 iReset.Statements.Add(new CodeMethodInvokeExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),"baseEnumerator"),"Reset",new CodeExpression[] {} ) ) ;
enumeratorClass.Members.Add(iReset) ;
#endregion

return enumeratorClass;
}


private static CodeIterationStatement CreateForLoop(string valPropertyName)
 {
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) )),
 new CodeStatement[] { new CodeExpressionStatement( new CodeMethodInvokeExpression(new CodeThisReferenceExpression(),"Add",new CodeIndexerExpression(new CodeArgumentReferenceExpression("val"),new CodeVariableReferenceExpression("i")) ) ) });
return forLoop;
}

private static CodeConstructor CreateConstructor(string collectionClassName, string paraEnd,string paramInfo)
 {
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)
 {
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;
}


 /**//// <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)
 {
CodeDomProvider provider;
switch(codeType)
 {
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();
}
 /**//// <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)
 {
TextWriter tw= new StreamWriter(fileName, false);
Make(itemTypeName,nameSpace,collectionClassName,codeType,tw);
tw.Close();
}
 /**//// <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)
 {
string tempfile=System.IO.Path.GetTempFileName();
StrongTypeCollection.Make(itemTypeName ,nameSpace , collectionClassName ,codeType,tempfile) ;
string Result="";
using (StreamReader sr = new StreamReader(tempfile))
 {
for(int i=0;i<9;i++)
sr.ReadLine();
Result =sr.ReadToEnd();
}
System.IO.File.Delete(tempfile) ;
return Result;
}


 /**//// <summary>
/// 代码的类型
/// </summary>
public enum CodeType
 {
 /**//// <summary>
/// C#
/// </summary>
CSharp,
 /**//// <summary>
/// VB.NET
/// </summary>
VB,
 /**//// <summary>
/// J#
/// </summary>
JSharp
}



}
}
完整代码下载地址: https://files.cnblogs.com/twodays/CollectionMaker.rar
编译好的程序下载地址: https://files.cnblogs.com/twodays/CollectionMaker.zip
|