title: How can I run code from a string?
body: <p>I have a textbox in my program. In the textbox you can write the name of function/method you want to run as well as pass parameters. This is all parsed and via a big switch block if my case is filled I the code for that specific case, as well as passing the parameters. </p>
  
  <p>But what I really want to do is to <strong>use stringbuilder to build source code and then run it!</strong> </p>
  
  <p>This is an example of source code built with my stringbuilder.   </p>
  
  <pre><code> outlook outlooken = new outlook(appNwindow);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>  outlooken.createNewEmail(scriptarray[i][1],scriptarray[i][2],
    scriptarray[i][3],scriptarray[i][4]);
  </code></pre>
  
  <p>Using stringbuilder to create strings is no problem at all. But how do I run them?</p>
  
  <p>I have tested a lot, and managed to get everything in place but I think I'm missing something cuase my code always generates an error...</p>
  
  <p><strong>Here's my soruce code</strong></p>
  
  <pre><code> CodeDomProvider myCodeDomeProvider = CodeDomProvider.CreateProvider("CSharp");
   String [] referenceAssemblies = {"System.dll"};
   //  string myAssemblyName = "myAssembly.dll";
   CompilerParameters myCompilerparameters = 
   new CompilerParameters(referenceAssemblies);
  
          myCompilerparameters.GenerateExecutable = false;
          myCompilerparameters.GenerateInMemory = true;
  
    //*** Here's the sourcecode it want to compile
    string[] arr1 = new string[] { "outlook outlooken = new outlook(appNwindow);","outlooken.createNewEmail(scriptarray[i][1],scriptarray[i][2],scriptarray[i[3],scriptarray[i][4]);"};
  
          CompilerResults myResults = myCodeDomeProvider.CompileAssemblyFromSource(myCompilerparameters, arr1);
          string objectname = "testet";
          string method = "createNewEmail";
          object[] args = new object[2];
  
          args[0] = "to";
          args[1] = "CC";
  
          if (myResults.Errors.HasErrors)
  
           {
              StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
              foreach (CompilerError error in myResults.Errors)
              {
              errors.AppendFormat("Line {0},{1}\t: {2}\n",
              error.Line, error.Column, error.ErrorText);
              }
  
              throw new Exception(errors.ToString());
              }
              else
              {
              Assembly assem = myResults.CompiledAssembly;
              object instance = null;
  
              Type type = assem.GetType(objectname);
              MethodInfo miChk = type.GetMethod(method);
  
              if (!miChk.IsStatic)
              {
              instance = assem.CreateInstance(objectname);
              type = instance.GetType();
              }
  
              MethodInfo mi = type.GetMethod(method);
  
              mi.Invoke(instance, args);
              }
  </code></pre>
  
  <p>These are the errors I get when running it:</p>
  
  <p>Additional information: Compiler Errors :</p>
  
  <p>Line 1,1    : A namespace cannot directly contain members such as fields or methods</p>
  
  <p>Line 1,25   : Expected class, delegate, enum, interface, or struct</p>
  
  <p>Line 1,1    : A namespace cannot directly contain members such as fields or methods</p>
  
  <p>Line 1,41   : Identifier expected</p>
  
  <p>Line 1,59   : Identifier expected</p>
  
  <p>Line 1,77   : Identifier expected</p>
  
  <p>Line 1,95   : Identifier expected</p>