title: (C#) Compiling class at runtime and calling methods from original code
body: <p><br/>
  I'm trying to compile code at runtime in C#, then from the compiled code call a function or initialize a class which is defined in the original code. <br/>
  The code I currently have:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Microsoft.CSharp;
  using System.CodeDom.Compiler;
  using System.Reflection;
  
  namespace CTFGame
  {
      class Program
      {
          static void Main(string[] args)
          {
              string code = @"
  
  using System;
  
  namespace CTFGame
  {
      public class MyPlayer
      {
          public static void Main ()
          {
              Console.WriteLine(""Hello world"");
          }
          /*public void DoTurn ()
          {
              Program.SayHello();
          }*/
      }
  }
  
  ";
              CSharpCodeProvider provider = new CSharpCodeProvider();
              CompilerParameters parameters = new CompilerParameters();
              parameters.GenerateInMemory = true;
  
              CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
              if (results.Errors.HasErrors)
              {
                  string errors = "";
                  foreach (CompilerError error in results.Errors)
                  {
                      errors += string.Format("Error #{0}: {1}\n", error.ErrorNumber, error.ErrorText);
                  }
                  Console.Write(errors);
              }
              else
              {
                  Assembly assembly = results.CompiledAssembly;
                  Type program = assembly.GetType("CTFGame.MyPlayer");
                  MethodInfo main = program.GetMethod("Main");
                  main.Invoke(null, null);
              }
          }
  
          public static void SayHello()
          {
              Console.WriteLine("I'm awesome &gt;&lt;");
          }
      }
  }
  </code></pre>
  
  <p>Now, Running the runtime loaded method 'Main' is a success, and the message "Hello world" is printed. The problem starts here: in the original code I have a method called "SayHello". I want to call this method from my runtime loaded code. <br/>
  If I uncomment the "DoTurn" method, a compiler error will show in runtime:<br/></p>
  
  <pre><code>Error #CS0103: The name 'Program' does not exist in the current context
  </code></pre>
  
  <p><br/><br/>
  My question is - is this possible, and how? <br/><br/>
  Putting the runtime loaded code in the same namespace doesn't help (and that makes sense), so what is the correct way to do that? <br/><br/>
  Thanks.</p>