Click here to Skip to main content
15,894,343 members
Articles / Programming Languages / C#

Evaluate C# Code (Eval Function)

Rate me:
Please Sign up or sign in to vote.
4.74/5 (48 votes)
12 Oct 2005CPOL 418.9K   4.9K   130  
An example that provides an Eval function for compiling/evaluating C# code at runtime.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Threading;

namespace EvalCSCode
{
	/// <summary>
	/// Zusammenfassung f�r Form1.
	/// </summary>
    public class Form1 : System.Windows.Forms.Form {
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Erforderlich f�r die Windows Form-Designerunterst�tzung
			//
			InitializeComponent();
			//textBox5.Text = sToEval;

			//
			// TODO: F�gen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu
			//

            textBox6.Text = "if (((TextBox)oParam).BackColor == Color.Coral) { \r\n " +
            "    ((TextBox)oParam).BackColor = Color.PaleGreen; \r\n " +
            "    ((TextBox)oParam).Text = \"On\"; \r\n " +
            "} else { \r\n " +
            "    ((TextBox)oParam).BackColor = Color.Coral; \r\n " +
            "    ((TextBox)oParam).Text = \"Off\"; \r\n " +
            "}";
		}

		/// <summary>
		/// Die verwendeten Ressourcen bereinigen.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Vom Windows Form-Designer generierter Code
		/// <summary>
		/// Erforderliche Methode f�r die Designerunterst�tzung. 
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor ge�ndert werden.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.label9 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.button4 = new System.Windows.Forms.Button();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.button3 = new System.Windows.Forms.Button();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.textBox6 = new System.Windows.Forms.TextBox();
            this.button5 = new System.Windows.Forms.Button();
            this.textBox7 = new System.Windows.Forms.TextBox();
            this.textBox8 = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(8, 12);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(548, 469);
            this.tabControl1.TabIndex = 18;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.label9);
            this.tabPage1.Controls.Add(this.label8);
            this.tabPage1.Controls.Add(this.label7);
            this.tabPage1.Controls.Add(this.label6);
            this.tabPage1.Controls.Add(this.label5);
            this.tabPage1.Controls.Add(this.label4);
            this.tabPage1.Controls.Add(this.label3);
            this.tabPage1.Controls.Add(this.button4);
            this.tabPage1.Controls.Add(this.textBox5);
            this.tabPage1.Controls.Add(this.button3);
            this.tabPage1.Controls.Add(this.textBox4);
            this.tabPage1.Controls.Add(this.button2);
            this.tabPage1.Controls.Add(this.textBox3);
            this.tabPage1.Controls.Add(this.textBox2);
            this.tabPage1.Controls.Add(this.label1);
            this.tabPage1.Controls.Add(this.textBox1);
            this.tabPage1.Controls.Add(this.button1);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(540, 443);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Single";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.label11);
            this.tabPage2.Controls.Add(this.textBox8);
            this.tabPage2.Controls.Add(this.textBox7);
            this.tabPage2.Controls.Add(this.label2);
            this.tabPage2.Controls.Add(this.label10);
            this.tabPage2.Controls.Add(this.textBox6);
            this.tabPage2.Controls.Add(this.button5);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(540, 443);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Batch";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(0, 296);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(440, 40);
            this.label9.TabIndex = 34;
            this.label9.Text = resources.GetString("label9.Text");
            // 
            // label8
            // 
            this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label8.Location = new System.Drawing.Point(0, 280);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(192, 16);
            this.label8.TabIndex = 33;
            this.label8.Text = "Eval external reference (non-static)";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(0, 208);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(440, 32);
            this.label7.TabIndex = 32;
            this.label7.Text = "This example shows how to call a static function in a external namespace (from th" +
                "e evaluated code sight). Here we include the parent assembly for use by eval cod" +
                "e";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(0, 112);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(440, 32);
            this.label6.TabIndex = 31;
            this.label6.Text = "This example first displays what you submit as param in a MessageBox an then exec" +
                "utes  the  code. Includes are the same as in simple eval";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(0, 16);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(440, 32);
            this.label5.TabIndex = 30;
            this.label5.Text = "In this example you can test a simple c# command or execute short code snippets. " +
                "Most of the standard system includes are made but none to the parent assembly";
            // 
            // label4
            // 
            this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Location = new System.Drawing.Point(0, 192);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(192, 16);
            this.label4.TabIndex = 29;
            this.label4.Text = "Eval external reference (static)";
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(0, 96);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(120, 16);
            this.label3.TabIndex = 28;
            this.label3.Text = "Eval with param";
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(456, 336);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 27;
            this.button4.Text = "Eval code";
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // textBox5
            // 
            this.textBox5.Location = new System.Drawing.Point(0, 336);
            this.textBox5.Multiline = true;
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(440, 72);
            this.textBox5.TabIndex = 26;
            this.textBox5.Text = "EvalCSCode.EvalCSCode elc = new EvalCSCode.EvalCSCode();\nelc.callBFunc();";
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(456, 240);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 25;
            this.button3.Text = "Eval code";
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(0, 240);
            this.textBox4.Multiline = true;
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(440, 24);
            this.textBox4.TabIndex = 24;
            this.textBox4.Text = "EvalCSCode.EvalCSCode.callAFunc();";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(456, 152);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 23;
            this.button2.Text = "Eval code";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(0, 144);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 20);
            this.textBox3.TabIndex = 22;
            this.textBox3.Text = "Param1";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(104, 144);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(336, 32);
            this.textBox2.TabIndex = 21;
            this.textBox2.Text = "MessageBox.Show(oParam.ToString());\nMessageBox.Show(\"Hello Says Eval\");";
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(87, 16);
            this.label1.TabIndex = 20;
            this.label1.Text = "Simple Eval";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(0, 47);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(440, 32);
            this.textBox1.TabIndex = 19;
            this.textBox1.Text = "MessageBox.Show(\"Hello Says Eval\", \"HyFromEval\", MessageBoxButtons.OK, MessageBox" +
                "Icon.Information);";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(456, 56);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 18;
            this.button1.Text = "Eval code";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(0, 16);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(440, 32);
            this.label2.TabIndex = 34;
            this.label2.Text = "In this example you can test a simple c# command or execute short code snippets. " +
                "Most of the standard system includes are made but none to the parent assembly";
            // 
            // label10
            // 
            this.label10.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label10.Location = new System.Drawing.Point(0, 0);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(99, 16);
            this.label10.TabIndex = 33;
            this.label10.Text = "Batch Eval";
            // 
            // textBox6
            // 
            this.textBox6.Location = new System.Drawing.Point(3, 51);
            this.textBox6.Multiline = true;
            this.textBox6.Name = "textBox6";
            this.textBox6.Size = new System.Drawing.Size(440, 103);
            this.textBox6.TabIndex = 32;
            this.textBox6.Text = "MessageBox.Show(\"Hello Says Eval\", \"HyFromEval\", MessageBoxButtons.OK, MessageBox" +
                "Icon.Information);";
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(456, 56);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 31;
            this.button5.Text = "Start";
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // textBox7
            // 
            this.textBox7.BackColor = System.Drawing.Color.Coral;
            this.textBox7.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox7.Location = new System.Drawing.Point(9, 212);
            this.textBox7.Name = "textBox7";
            this.textBox7.Size = new System.Drawing.Size(522, 20);
            this.textBox7.TabIndex = 35;
            this.textBox7.Text = "Off";
            this.textBox7.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // textBox8
            // 
            this.textBox8.Location = new System.Drawing.Point(65, 190);
            this.textBox8.Name = "textBox8";
            this.textBox8.ReadOnly = true;
            this.textBox8.Size = new System.Drawing.Size(466, 20);
            this.textBox8.TabIndex = 36;
            this.textBox8.Text = "0";
            this.textBox8.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(6, 193);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(53, 13);
            this.label11.TabIndex = 37;
            this.label11.Text = "No. exec:";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(568, 489);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.tabPage2.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Der Haupteinstiegspunkt f�r die Anwendung.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//EvalCSCode elp = new EvalCSCode();
			EvalCSCode.Eval(textBox1.Text);
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			EvalCSCode.EvalWithParam(textBox2.Text,((object)textBox3.Text));
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			EvalCSCode.EvalWithRef(textBox4.Text);
		}

        private TabControl tabControl1;
        private TabPage tabPage1;
        private Label label9;
        private Label label8;
        private Label label7;
        private Label label6;
        private Label label5;
        private Label label4;
        private Label label3;
        private Button button4;
        private TextBox textBox5;
        private Button button3;
        private TextBox textBox4;
        private Button button2;
        private TextBox textBox3;
        private TextBox textBox2;
        private Label label1;
        private TextBox textBox1;
        private Button button1;
        private TabPage tabPage2;
        private Label label2;
        private Label label10;
        private TextBox textBox6;
        private Button button5;
        private TextBox textBox7;
        private TextBox textBox8;
        private Label label11;

		string sToEval = "EvalCSCode.EvalCSCode elc = new EvalCSCode.EvalCSCode();\nelc.callBFunc();";
		private void button4_Click(object sender, System.EventArgs e)
		{
			//string sTest = textBox5.Text;
			EvalCSCode.EvalWithRef(textBox5.Text);
		}

        private void button5_Click(object sender, EventArgs e) {
            if(button5.Text.Equals( "Start") )
                button5.Text = "Stop";
            else
                button5.Text = "Start";

            do {

                //toggleTxt();
                EvalCSCode.EvalWithParam2(textBox6.Text, ((object)textBox7));
                try{
                    textBox8.Text = ((int)int.Parse(textBox8.Text)+1).ToString();
                }catch(Exception ex){
                }

                Application.DoEvents();
                Thread.Sleep(100);
            } while (button5.Text.Equals("Stop"));
        }

        private void toggleTxt() {
            if (textBox7.BackColor == Color.Coral) {
                textBox7.BackColor = Color.PaleGreen;
                textBox7.Text = "On";
            } else {
                textBox7.BackColor = Color.Coral;
                textBox7.Text = "Off";
            }
        }
	}
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
programmer and software junkie since 1991 zurich switzerland

Comments and Discussions