Click here to Skip to main content
Licence CPOL
First Posted 12 Oct 2005
Views 148,772
Downloads 1,314
Bookmarked 98 times

Evaluate C# Code (Eval Function)

By | 12 Oct 2005 | Article
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)

About the Author

kim.david.hauser

Software Developer (Senior)

Switzerland Switzerland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow do you replace a string at a given position with it's working code representative? PinmemberLarissa Schön0:47 17 Mar '12  
GeneralAwesome, though quick question Pinmembermbainrot21:03 10 Apr '09  
QuestionEvalToObject Question Pinmembergravelkm2:43 14 Nov '08  
Questioncan a web project use "ReferencedAssemblies.Add("customer.dll")" Pinmemberwlbkeats23:34 7 Dec '06  
GeneralUses of Eval PinmemberAnup Shinde16:06 29 Sep '06  
GeneralTotal waste of time... Pinmembermarkman@shaw.ca12:08 3 Mar '06  
GeneralRe: Total waste of time... PinmemberPascal Ganaye9:12 20 Mar '06  
GeneralRe: Total waste of time... PinPopularmembermarkman@shaw.ca5:54 21 Mar '06  
GeneralRe: Total waste of time... PinmemberGraham Harrison23:34 11 Jan '07  
GeneralAn Eval Function for C# using JScript.NET (JavaScript) PinmemberMichael Freidgeim0:48 5 Jan '11  
GeneralRe: Total waste of time... Pinmemberkim.hauser2:37 8 Feb '07  
GeneralRe: Total waste of time... Pinmembermarketware11:25 30 Aug '07  
GeneralRe: Total waste of time... Pinmembermarketware11:08 31 Aug '07  
GeneralI dont think Total waste of time.. PinmemberMidax9:40 27 Apr '10  
QuestionUses PinmemberEvilToilet9:15 23 Oct '05  
AnswerRe: Uses Pinmemberkim.hauser11:03 2 Nov '05  
GeneralThanks for review... Pinmemberkim.hauser10:13 21 Oct '05  
QuestionUsing a static method instead Pinmemberanonymous20:07 19 Oct '05  
AnswerRe: Using a static method instead Pinmemberaprenot10:17 21 Oct '05  
GeneralRe: Using a static method instead PinmemberToli Cuturicu11:22 20 Dec '10  
GeneralExcellent! But Pinmembercccccbbbbb14:41 19 Oct '05  
GeneralRe: Excellent! But Pinmemberkim.hauser0:29 22 Oct '05  
AnswerRe: Excellent! But PinmemberKevan Davis11:18 1 Dec '06  
GeneralExcellent !! PinmemberTittle Joseph19:46 18 Oct '05  
GeneralThanks, but ... PinmemberHerman Chelette2:33 18 Oct '05  

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.

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