You do not have much time to give you the correct answer, but here is the eval code from one of my old projects:
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{ " + csCode + "} }"); 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("Transform"); return mi;
The original version of this code was taken from: http://www.codeproject.com/KB/cs/evalcscode.aspx
It compiles some C # code (the code should be in the csCode variable), then tries to find the Transform () method in it and returns its MethodInfo, so we can execute it.
But remember that every time you call this code, a new assembly is loaded, so do not use it too often.
Also, as Prescott said, try Roslyn
Hope this helps. Sorry for not providing a code example more specific to your question.
PS. If you want some kind of interactive window, there are third-party controls for this (use Google to find, because I do not remember the exact names of the products)
source share