Click here to Skip to main content
13,293,178 members (42,901 online)
Click here to Skip to main content
Articles » Languages » C# » Reflection » Downloads


44 bookmarked
Posted 6 May 2007

Dynamic Assemblies using Reflection.Emit. Part II of II - Reflection.Emit

, 6 May 2007
This article explains how Reflection.Emit classes can be used to dynamically generate .NET assemblies.
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;

namespace DynamicCodeGeneration.CodeGenerator
    class CSharpGenerator : IGenerator
        private string _code;

        /// <summary>
        /// </summary>
        /// <returns></returns>
        private string BuildGeneratedCode()
                CSharpCodeProvider codeProvider = new CSharpCodeProvider();
                ICodeCompiler codeCompiler = codeProvider.CreateCompiler();
                CompilerParameters parameters = new CompilerParameters();

                parameters.GenerateInMemory = false;

                CompilerResults results = codeCompiler.CompileAssemblyFromSource(parameters, this.Code);

                if (results.Errors.HasErrors)
                    string errorMessage = "";
                    errorMessage = results.Errors.Count.ToString() + " Errors:";
                    for (int x = 0; x < results.Errors.Count; x++)
                        errorMessage = errorMessage + "\r\nLine: " + results.Errors[x].Line.ToString() + " - " + results.Errors[x].ErrorText;
                    return errorMessage;
                return results.PathToAssembly;
            catch (Exception ex)
                throw new Exception("Error while trying to build generated code.", ex);

        #region IGenerator Members

        /// <summary>
        /// </summary>
        /// <param name="assemblyAuthorName"></param>
        /// <param name="classAuthorName"></param>
        /// <param name="methodAuthorName"></param>
        /// <returns>Generated Code</returns>
        public string GenerateAssembly(string assemblyAuthorName, string classAuthorName, string methodAuthorName)
                #region Unit
                CodeCompileUnit unit = new CodeCompileUnit();
                CodeAttributeArgument[] arguments = new CodeAttributeArgument[2];
                arguments[0] = new CodeAttributeArgument(new CodePrimitiveExpression(assemblyAuthorName));//Create parameter for attribute
                arguments[1] = new CodeAttributeArgument(new CodeSnippetExpression("DynamicCodeGeneration.CustomAttributes.GenerationMode.CodeDOM"));
                CodeAttributeDeclaration assemblyLevelAttribute = new CodeAttributeDeclaration(new CodeTypeReference("DynamicCodeGeneration.CustomAttributes.AssemblyLevelAttribute"), arguments);//Create attribute to be added to assembly


                #region Namespace
                CodeNamespace customEntityRoot = new CodeNamespace("DerivedRoot");//Create a namespace


                customEntityRoot.Imports.Add(new CodeNamespaceImport("System"));//Add references
                customEntityRoot.Imports.Add(new CodeNamespaceImport("DynamicCodeGeneration.Base"));//Add references
                customEntityRoot.Imports.Add(new CodeNamespaceImport("DynamicCodeGeneration.CustomAttributes"));//Add references

                #region Class
                CodeTypeDeclaration derived = new CodeTypeDeclaration("Derived");//Create class

                customEntityRoot.Types.Add(derived);//Add the class to namespace defined above

                CodeConstructor derivedClassConstructor = new CodeConstructor();//Create constructor
                derivedClassConstructor.Attributes = MemberAttributes.Public;

                derived.Members.Add(derivedClassConstructor);//Add constructor to class

                CodeAttributeArgument argument = new CodeAttributeArgument(new CodePrimitiveExpression(classAuthorName));
                CodeAttributeDeclaration classLevelAttribute = new CodeAttributeDeclaration(new CodeTypeReference("DynamicCodeGeneration.CustomAttributes.ClassLevelAttribute"), argument);//Create attribute to be added to class


                derived.BaseTypes.Add(new CodeTypeReference("Base"));

                #region Method
                CodeMemberMethod derivedMethod = new CodeMemberMethod();
                derivedMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override; //Make this method an override of base class's method
                derivedMethod.Comments.Add(new CodeCommentStatement(new CodeComment("TestComment")));
                derivedMethod.Name = "Method";
                derivedMethod.ReturnType = new CodeTypeReference(typeof(void));
                arguments = new CodeAttributeArgument[2];
                arguments[0] = new CodeAttributeArgument(new CodeSnippetExpression("ComplexityLevel.SuperComplex"));//Create parameter for attribute
                arguments[1] = new CodeAttributeArgument(new CodePrimitiveExpression(methodAuthorName));
                CodeAttributeDeclaration methodLevelAttribute = new CodeAttributeDeclaration(new CodeTypeReference("DynamicCodeGeneration.CustomAttributes.MethodLevelAttribute"), arguments);//Create attribute to be added to method

                derivedMethod.CustomAttributes.Add(methodLevelAttribute);//Add attribute to method

                CodeSnippetStatement code = new CodeSnippetStatement("base.Method();");
                derived.Members.Add(derivedMethod);//Add method to the class

                #region Generate Code
                CSharpCodeProvider codeProvider = new CSharpCodeProvider();
                ICodeGenerator codeGenerator = codeProvider.CreateGenerator();

                StringBuilder generatedCode = new StringBuilder();
                StringWriter codeWriter = new StringWriter(generatedCode);
                CodeGeneratorOptions options = new CodeGeneratorOptions();
                options.BracingStyle = "C";//Keep the braces on the line following the statement or declaration that they are associated with
                codeGenerator.GenerateCodeFromCompileUnit(unit, codeWriter, options);

                this.Code = generatedCode.ToString();
                return BuildGeneratedCode();
            catch (Exception ex)
                throw new Exception("Error while trying to generate code.", ex);

        /// <summary>
        /// </summary>
        public string Code
            get { return _code; }
            set { _code = value; }


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Piyush S Bhatnagar
Web Developer
United States United States
I work with Proteans Software Solutions. Interests include software architecture, design patterns, agile, scrum development, automated acceptance testing, books, music, travel, movies...

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 6 May 2007
Article Copyright 2007 by Piyush S Bhatnagar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid