string code = txtCode.Text;
/* code值:
public class MyClass{
public static string Show(){
return "HEllo";
}
}
*/
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
CodeDomProvider comp = new CSharpCodeProvider();
//编译代码
CompilerResults cr=comp.CompileAssemblyFromSource(cp, code);if (cr.Errors.HasErrors)
{
lblResult.Text = cr.Errors.ToString();
}
else {
//执行静态方法
Assembly a = cr.CompiledAssembly;Type t = a.GetType("MyClass");
MethodInfo mi = t.GetMethod("Show");
lblResult.Text=mi.Invoke(a,null).ToString();
}