Click here to Skip to main content
11,575,109 members (60,598 online)
Click here to Skip to main content

Evaluate C# Code (Eval Function)

, 12 Oct 2005 CPOL 247K 3K 119
Rate this:
Please Sign up or sign in to vote.
An example that provides an Eval function for compiling/evaluating C# code at runtime.

Sample Image - EvalCSCode.jpg

Introduction

This example shows an "Eval" function for C# and implements different usages of this evaluate function. The library is written in C# and can be tested with the MainForm in the solution.

Source view

Here you will see all the trick. The code generates a class structure with a basic "function prototype" in which the code to evaluate is filled in. The execution of the function should execute your code:

// Eval > Evaluates C# sourcelanguage
public static object Eval(string sCSCode) {

  CSharpCodeProvider c = new CSharpCodeProvider();
  ICodeCompiler icc = c.CreateCompiler();
  CompilerParameters cp = new CompilerParameters();

  cp.ReferencedAssemblies.Add("system.dll");
  cp.ReferencedAssemblies.Add("system.xml.dll");
  cp.ReferencedAssemblies.Add("system.data.dll");
  cp.ReferencedAssemblies.Add("system.windows.forms.dll");
  cp.ReferencedAssemblies.Add("system.drawing.dll");

  cp.CompilerOptions = "/t:library";
  cp.GenerateInMemory = true;

  StringBuilder sb = new StringBuilder("");
  sb.Append("using System;\n" );
  sb.Append("using System.Xml;\n");
  sb.Append("using System.Data;\n");
  sb.Append("using System.Data.SqlClient;\n");
  sb.Append("using System.Windows.Forms;\n");
  sb.Append("using System.Drawing;\n");

  sb.Append("namespace CSCodeEvaler{ \n");
  sb.Append("public class CSCodeEvaler{ \n");
  sb.Append("public object EvalCode(){\n");
  sb.Append("return "+sCSCode+"; \n");
  sb.Append("} \n");
  sb.Append("} \n");
  sb.Append("}\n");

  CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
  if( cr.Errors.Count > 0 ){
      MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText, 
         "Error evaluating cs code", MessageBoxButtons.OK, 
         MessageBoxIcon.Error );
      return null;
  }

  System.Reflection.Assembly a = cr.CompiledAssembly;
  object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");

  Type t = o.GetType();
  MethodInfo mi = t.GetMethod("EvalCode");

  object s = mi.Invoke(o, null);
  return s;

}

Thanks fly out to:

Credits:

  • Kim Hauser (dave) - Application Developer & System Engineer.

License

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

Share

About the Author

kim.david.hauser
Software Developer (Senior)
Switzerland Switzerland
programmer and software junkie since 1991 zurich switzerland

You may also be interested in...

Comments and Discussions

 
QuestionNot work Pin
Member 1178584723-Jun-15 2:30
memberMember 1178584723-Jun-15 2:30 
QuestionExecuting code not in same class Namespace Pin
p3ptools23-Apr-15 8:33
memberp3ptools23-Apr-15 8:33 
QuestionEval LINQ expersion Pin
FarazLoloei25-Oct-14 3:02
memberFarazLoloei25-Oct-14 3:02 
QuestionGood work Pin
Sm.Abdullah6-Oct-12 20:13
memberSm.Abdullah6-Oct-12 20:13 
QuestionHow do you replace a string at a given position with it's working code representative? Pin
Larissa Schön17-Mar-12 0:47
memberLarissa Schön17-Mar-12 0:47 
GeneralAwesome, though quick question Pin
mbainrot10-Apr-09 21:03
membermbainrot10-Apr-09 21:03 
QuestionEvalToObject Question Pin
gravelkm14-Nov-08 2:43
membergravelkm14-Nov-08 2:43 
Questioncan a web project use "ReferencedAssemblies.Add("customer.dll")" Pin
wlbkeats7-Dec-06 23:34
memberwlbkeats7-Dec-06 23:34 
AnswerRe: can a web project use "ReferencedAssemblies.Add("customer.dll")" Pin
lzuxiaolong27-Nov-12 15:13
memberlzuxiaolong27-Nov-12 15:13 
GeneralUses of Eval Pin
Anup Shinde29-Sep-06 16:06
memberAnup Shinde29-Sep-06 16:06 
GeneralTotal waste of time... Pin
markman@shaw.ca3-Mar-06 12:08
membermarkman@shaw.ca3-Mar-06 12:08 
GeneralRe: Total waste of time... Pin
Pascal Ganaye20-Mar-06 9:12
memberPascal Ganaye20-Mar-06 9:12 
GeneralRe: Total waste of time... Pin
markman@shaw.ca21-Mar-06 5:54
membermarkman@shaw.ca21-Mar-06 5:54 
GeneralRe: Total waste of time... Pin
Graham Harrison11-Jan-07 23:34
memberGraham Harrison11-Jan-07 23:34 
GeneralAn Eval Function for C# using JScript.NET (JavaScript) Pin
Michael Freidgeim5-Jan-11 0:48
memberMichael Freidgeim5-Jan-11 0:48 
GeneralRe: Total waste of time... Pin
kim.hauser8-Feb-07 2:37
memberkim.hauser8-Feb-07 2:37 
GeneralRe: Total waste of time... Pin
marketware30-Aug-07 11:25
membermarketware30-Aug-07 11:25 
GeneralRe: Total waste of time... Pin
marketware31-Aug-07 11:08
membermarketware31-Aug-07 11:08 
GeneralI dont think Total waste of time.. Pin
Midax27-Apr-10 9:40
memberMidax27-Apr-10 9:40 
QuestionUses Pin
EvilToilet23-Oct-05 9:15
memberEvilToilet23-Oct-05 9:15 
AnswerRe: Uses Pin
kim.hauser2-Nov-05 11:03
memberkim.hauser2-Nov-05 11:03 
GeneralThanks for review... Pin
kim.hauser21-Oct-05 10:13
memberkim.hauser21-Oct-05 10:13 
QuestionUsing a static method instead Pin
anonymous19-Oct-05 20:07
memberanonymous19-Oct-05 20:07 
AnswerRe: Using a static method instead Pin
aprenot21-Oct-05 10:17
memberaprenot21-Oct-05 10:17 
GeneralRe: Using a static method instead Pin
Toli Cuturicu20-Dec-10 11:22
memberToli Cuturicu20-Dec-10 11:22 
GeneralExcellent! But Pin
cccccbbbbb19-Oct-05 14:41
membercccccbbbbb19-Oct-05 14:41 
GeneralRe: Excellent! But Pin
kim.hauser22-Oct-05 0:29
memberkim.hauser22-Oct-05 0:29 
AnswerRe: Excellent! But Pin
Kevan Davis1-Dec-06 11:18
memberKevan Davis1-Dec-06 11:18 
GeneralExcellent !! Pin
Tittle Joseph18-Oct-05 19:46
memberTittle Joseph18-Oct-05 19:46 
GeneralThanks, but ... Pin
Herman Chelette18-Oct-05 2:33
memberHerman Chelette18-Oct-05 2:33 
GeneralRe: Thanks, but ... Pin
kim.hauser21-Oct-05 9:34
memberkim.hauser21-Oct-05 9:34 
GeneralGreat Job Pin
aprenot13-Oct-05 5:02
memberaprenot13-Oct-05 5:02 
GeneralRe: Great Job Pin
Anonymous21-Oct-05 9:52
sussAnonymous21-Oct-05 9:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 13 Oct 2005
Article Copyright 2005 by kim.david.hauser
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid