Sometimes customers requires the ability to write small pieces of custom code that should be run from within your application. And not only that, you want to be able to get resulting variables from that user code, inside your application.
An example could be an advanced calculator, where some of the algorithms are defined by the user. Of course there could be a big security issue in letting users define code that are allowed to run within your application, but issues related to security is not discussed here.

Check this example:

First define an interface the describes the properties and methods that are accessable by the customers code:

namespace ScriptInterface {
    public interface ILiveScript {
        void Run();
        int Input { set;}
        int Output { get;}
    }
}

The interface should be defined in an assemble (here ScriptInterface.dll) seperated from the actual compiler, we come back to that later.

Then define the actual scriptcompiler class:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
using ScriptInterface;

namespace ScriptRunner {
    public class Calculator {

        internal static string scriptHeader =
            "using System;" +
            "using ScriptInterface;" +
            "namespace ScriptRunner {" +
                "class LiveScript : ILiveScript {" +
                    "private int input;" +
                    "private int output;" +
                    "public void Run() {";

        internal static string scriptFooter =
                    "}" +
                    "public int Input {" +
                        "set { input = value; }" +
                        "get { return input; }" +
                    "}" +
                    "public int Output {" +
                        "set { output = value; }" +
                        "get { return output; }" +
                    "}" +
                "}" +
            "}";

        public static ILiveScript ExecuteScript(string source) {
            string[] mergedSource = new string[] { scriptHeader + source + scriptFooter };
            CompilerResults results = CompileSource(mergedSource);
            if (!results.Errors.HasErrors) {
                return RunScript(results.CompiledAssembly);
            } else {
                throw new Exception(GenerateCompileErrorLines(results.Errors));
            }
        }

        private static string GenerateCompileErrorLines(CompilerErrorCollection errors) {
            StringBuilder sb = new StringBuilder();
            sb.Append("\r\n");
            foreach (CompilerError error in errors) {
                sb.Append(error.Line).Append(": ").Append(error.ErrorText).Append("\r\n");
            }
            return sb.ToString();
        }

        private static ILiveScript RunScript(Assembly compiledAssembly) {
            ILiveScript method = (ILiveScript)compiledAssembly.CreateInstance("ScriptRunner.LiveScript", true);
            method.Input = 7; // whatever, just an example
            try {
                method.Run();
                return method;
            } catch (Exception ex) {
                throw new Exception("Running of custom script failed.", ex);
            }
        }

        private static CompilerResults CompileSource(string[] source) {
            CodeDomProvider provider = new CSharpCodeProvider(); //or VBCodeProvider()
            CompilerParameters cp = new CompilerParameters();
            cp.ReferencedAssemblies.Add("system.dll"); //includes
            cp.ReferencedAssemblies.Add("ScriptInterface.dll"); //includes our own interface!
            cp.GenerateExecutable = false; //generate executable
            return provider.CompileAssemblyFromSource(cp, source);
        }

    }
}

As you see the actual compiled usercode is a welldefined header and footer merged with the users own code. Since the merged code implements our interface, you can control which properties the usercode can access, and you can read resulting properties from that code.

So the actual usage of this, could look like this:

using System;
using ScriptInterface;
using ScriptRunner;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            ILiveScript method = Calculator.ExecuteScript("Output = Input * 2;"); // could come from args
            Console.Write("Result:" + method.Output);
        }
    }
}

Have fun with your own userenabled compiler application.