Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. runtime compile problem

runtime compile problem

Scheduled Pinned Locked Moved C#
csharphelptutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sduhd
    wrote on last edited by
    #1

    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:

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups