Click here to Skip to main content
6,594,432 members and growing! (17,062 online)
Email Password   helpLost your password?
Languages » C# » Applications     Intermediate License: The Code Project Open License (CPOL)

Evaluate C# Code (Eval Function)

By kim.david.hauser

An example that provides an Eval function for compiling/evaluating C# code at runtime.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:12 Oct 2005
Views:80,561
Bookmarked:68 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
28 votes for this article.
Popularity: 6.11 Rating: 4.22 out of 5
2 votes, 7.1%
1
2 votes, 7.1%
2
1 vote, 3.6%
3
6 votes, 21.4%
4
17 votes, 60.7%
5

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


Member

Occupation: Web Developer
Location: Switzerland Switzerland

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
GeneralAwesome, though quick question Pinmembermbainrot22:03 10 Apr '09  
QuestionEvalToObject Question Pinmembergravelkm3:43 14 Nov '08  
Generalcan a web project use "ReferencedAssemblies.Add("customer.dll")" Pinmemberwlbkeats0:34 8 Dec '06  
GeneralUses of Eval PinmemberAnup Shinde17:06 29 Sep '06  
GeneralTotal waste of time... Pinmembermarkman@shaw.ca13:08 3 Mar '06  
GeneralRe: Total waste of time... PinmemberPascal Ganaye10:12 20 Mar '06  
GeneralRe: Total waste of time... Pinmembermarkman@shaw.ca6:54 21 Mar '06  
GeneralRe: Total waste of time... PinmemberGraham Harrison0:34 12 Jan '07  
GeneralRe: Total waste of time... Pinmemberkim.hauser3:37 8 Feb '07  
GeneralRe: Total waste of time... Pinmembermarketware12:25 30 Aug '07  
GeneralRe: Total waste of time... Pinmembermarketware12:08 31 Aug '07  
QuestionUses PinmemberEvilToilet10:15 23 Oct '05  
AnswerRe: Uses Pinmemberkim.hauser12:03 2 Nov '05  
GeneralThanks for review... Pinmemberkim.hauser11:13 21 Oct '05  
QuestionUsing a static method instead Pinmemberanonymous21:07 19 Oct '05  
AnswerRe: Using a static method instead Pinmemberaprenot11:17 21 Oct '05  
GeneralExcellent! But Pinmembercccccbbbbb15:41 19 Oct '05  
GeneralRe: Excellent! But Pinmemberkim.hauser1:29 22 Oct '05  
AnswerRe: Excellent! But PinmemberKevan Davis12:18 1 Dec '06  
GeneralExcellent !! PinmemberTittle Joseph20:46 18 Oct '05  
GeneralThanks, but ... PinmemberHerman Chelette3:33 18 Oct '05  
GeneralRe: Thanks, but ... Pinmemberkim.hauser10:34 21 Oct '05  
GeneralGreat Job Pinmemberaprenot6:02 13 Oct '05  
GeneralRe: Great Job PinsussAnonymous10:52 21 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Oct 2005
Editor: Paul Conrad
Copyright 2005 by kim.david.hauser
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project