CodeDOM:集合类生成器

写程序的时候,写那些集合类真是麻烦,如果不自己写的话,通常我们会使用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();


            
增加文件的整体信息

            
增加集合类


            
return CompileUnit;
        }


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

            
私有变量

            
构造函数

            
Current属性

            
实现IEnumerator的Current属性

            
MoveNext方法

            
实现IEnumerator的MoveNext方法

            
Reset方法

            
实现IEnumerator的Reset方法

            
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
        }




    }

}



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

你可能感兴趣的:(code)