C# Runtime Compilation with CSharpCodeProvider
<p>I have had success using this tutorial: <a href="http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime" rel="nofollow">http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime</a> to set up a framework for runtime compilation and execution of C# code. Below is the code I currently have:</p>

<pre><code>public static class CodeCompiler {

public static object InterpretString(string executable) {
    string compilation_string = 
    @"
    static class RuntimeCompilationCode { 
        public static void Main() {}  
        public static object Custom() {
            /* CODE HERE */
        }
    }";

    compilation_string = compilation_string.Replace("/* CODE HERE */", executable);

    CSharpCodeProvider provider = new CSharpCodeProvider();
    CompilerParameters compiler_parameters = new CompilerParameters();

    // True - memory generation, false - external file generation
    compiler_parameters.GenerateInMemory = true;

    // True - exe file generation, false - dll file generation
    compiler_parameters.GenerateExecutable = true;

    // Compile
    CompilerResults results = provider.CompileAssemblyFromSource(compiler_parameters, compilation_string);

    // Check errors
    if (results.Errors.HasErrors) {
        StringBuilder builder = new StringBuilder();
        foreach (CompilerError error in results.Errors) {
            builder.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
        }
        throw new InvalidOperationException(builder.ToString());
    }

    // Execute
    Assembly assembly = results.CompiledAssembly;
    Type program = assembly.GetType("RuntimeCompilationCode");
    MethodInfo execute = program.GetMethod("Custom");
    return execute.Invoke(null, null);
}

}
</code></pre>

<p>I can pass a statement in the form of a string (ex. <code>"return 2;"</code>) to <code>InterpretString()</code> and it will be compiled and executed as part of the <code>Custom()</code> function. However I am wondering if it is possible to use the same approach to execute a method that is in my original file. For instance, suppose the  <code>CodeCompiler</code> class had another method <code>returnsTwo()</code> which returns the integer 2. Is there a way to call such a method by passing <code>"CodeCompiler.returnsTwo();"</code> or a similar string to <code>InterpretString()</code>?</p>