Click here to Skip to main content
15,891,567 members
Articles / Programming Languages / C#

Dynamic Code Generation Using CodeDOM

Rate me:
Please Sign up or sign in to vote.
4.46/5 (21 votes)
1 Jul 2007Ms-PL12 min read 100.5K   2.4K   67  
This article explains how CodeDOM can be used to generate code dynamcially and build it using dynamic code compilation. It also explains how to apply custom attributes.
namespace DynamicCodeGeneration.CodeGenerator
{
    partial class UI
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UI));
            this.Do = new System.Windows.Forms.Button();
            this.AssemblyAuthor = new System.Windows.Forms.TextBox();
            this.GeneratedCode = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.AssemblyLocation = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.CSharp = new System.Windows.Forms.RadioButton();
            this.IL = new System.Windows.Forms.RadioButton();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.ClassAuthor = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.MethodAuthor = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // Do
            // 
            this.Do.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Do.Location = new System.Drawing.Point(535, 7);
            this.Do.Name = "Do";
            this.Do.Size = new System.Drawing.Size(86, 23);
            this.Do.TabIndex = 0;
            this.Do.Text = "Generate";
            this.Do.UseVisualStyleBackColor = true;
            this.Do.Click += new System.EventHandler(this.Do_Click);
            // 
            // AssemblyAuthor
            // 
            this.AssemblyAuthor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.AssemblyAuthor.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AssemblyAuthor.Location = new System.Drawing.Point(141, 5);
            this.AssemblyAuthor.Name = "AssemblyAuthor";
            this.AssemblyAuthor.Size = new System.Drawing.Size(136, 24);
            this.AssemblyAuthor.TabIndex = 1;
            this.AssemblyAuthor.Text = "Neo";
            // 
            // GeneratedCode
            // 
            this.GeneratedCode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.GeneratedCode.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.GeneratedCode.Location = new System.Drawing.Point(141, 148);
            this.GeneratedCode.Multiline = true;
            this.GeneratedCode.Name = "GeneratedCode";
            this.GeneratedCode.ReadOnly = true;
            this.GeneratedCode.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.GeneratedCode.Size = new System.Drawing.Size(470, 359);
            this.GeneratedCode.TabIndex = 2;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(22, 7);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(116, 17);
            this.label1.TabIndex = 3;
            this.label1.Text = "Assembly Author:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(26, 148);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(112, 17);
            this.label2.TabIndex = 4;
            this.label2.Text = "Generated Code:";
            // 
            // AssemblyLocation
            // 
            this.AssemblyLocation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.AssemblyLocation.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AssemblyLocation.Location = new System.Drawing.Point(141, 106);
            this.AssemblyLocation.Name = "AssemblyLocation";
            this.AssemblyLocation.ReadOnly = true;
            this.AssemblyLocation.Size = new System.Drawing.Size(470, 24);
            this.AssemblyLocation.TabIndex = 5;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(1, 108);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(137, 17);
            this.label3.TabIndex = 6;
            this.label3.Text = "Generated Assembly:";
            // 
            // CSharp
            // 
            this.CSharp.AutoSize = true;
            this.CSharp.Checked = true;
            this.CSharp.Location = new System.Drawing.Point(445, 7);
            this.CSharp.Name = "CSharp";
            this.CSharp.Size = new System.Drawing.Size(48, 21);
            this.CSharp.TabIndex = 7;
            this.CSharp.TabStop = true;
            this.CSharp.Text = "C#";
            this.CSharp.UseVisualStyleBackColor = true;
            // 
            // IL
            // 
            this.IL.AutoSize = true;
            this.IL.Location = new System.Drawing.Point(489, 8);
            this.IL.Name = "IL";
            this.IL.Size = new System.Drawing.Size(40, 21);
            this.IL.TabIndex = 8;
            this.IL.Text = "IL";
            this.IL.UseVisualStyleBackColor = true;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Location = new System.Drawing.Point(287, 8);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(152, 17);
            this.label4.TabIndex = 9;
            this.label4.Text = "Code Generation Mode:";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(49, 34);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(89, 17);
            this.label5.TabIndex = 11;
            this.label5.Text = "Class Author:";
            // 
            // ClassAuthor
            // 
            this.ClassAuthor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.ClassAuthor.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ClassAuthor.Location = new System.Drawing.Point(141, 32);
            this.ClassAuthor.Name = "ClassAuthor";
            this.ClassAuthor.Size = new System.Drawing.Size(136, 24);
            this.ClassAuthor.TabIndex = 10;
            this.ClassAuthor.Text = "Morpheus";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label6.Location = new System.Drawing.Point(30, 64);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(100, 17);
            this.label6.TabIndex = 13;
            this.label6.Text = "Method Author";
            // 
            // MethodAuthor
            // 
            this.MethodAuthor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.MethodAuthor.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.MethodAuthor.Location = new System.Drawing.Point(141, 62);
            this.MethodAuthor.Name = "MethodAuthor";
            this.MethodAuthor.Size = new System.Drawing.Size(136, 24);
            this.MethodAuthor.TabIndex = 12;
            this.MethodAuthor.Text = "Trinity";
            // 
            // UI
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 17F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(654, 524);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.MethodAuthor);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.ClassAuthor);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.IL);
            this.Controls.Add(this.CSharp);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.AssemblyLocation);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.GeneratedCode);
            this.Controls.Add(this.AssemblyAuthor);
            this.Controls.Add(this.Do);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "UI";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Dynamic Assembly Generator";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button Do;
        private System.Windows.Forms.TextBox AssemblyAuthor;
        private System.Windows.Forms.TextBox GeneratedCode;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox AssemblyLocation;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.RadioButton CSharp;
        private System.Windows.Forms.RadioButton IL;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox ClassAuthor;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox MethodAuthor;
    }
}

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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
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...

Comments and Discussions