runtime compile problem
-
I have several self-contained C# samples, and I want to show them run to students in class. So I wrote a program, in which I opened the source file, and then .... Microsoft.CSharp.CSharpCodeProvider cp = new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler ic = cp.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters cpar = new System.CodeDom.Compiler.CompilerParameters(); cpar.ReferencedAssemblies.Add("system.dll"); string src = this.textBox1.Text; System.CodeDom.Compiler.CompilerResults cr = ic.CompileAssemblyFromSource(cpar,src); foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) MessageBox.Show(ce.ErrorText); if (cr.Errors.Count == 0 && cr.CompiledAssembly != null) { Type ObjType = cr.CompiledAssembly.GetType(classname); try { if (ObjType != null) { string[] arguments={}; myobj = Activator.CreateInstance(ObjType); ObjType.InvokeMember("Main", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static|BindingFlags.IgnoreCase, null, null, arguments); } } catch (Exception ex) { MessageBox.Show(ex.Message); } return true; } My problem is when I run it, I always catch the exception MissingMethodException, i.e. the method "Main" cannot be found. In textBox2, I input "P2_1.FirstProgram", that's the class which hosted the method "Main". I guess the "ObjType.InvokeMember" caused the problem, but MSDN did not tell me how to do clearly. Help!!!!!:confused: