TT.Template.Generate C# (CSharp) Method

Generate() static private method

static private Generate ( string code, string name, bool inMemory ) : Assembly
code string
name string
inMemory bool
return System.Reflection.Assembly
        static Assembly Generate(string code, string name, bool inMemory)
        {
            inMemory = true;
            //TODO - allow specification of output dir
            string outputDir = Directory.GetCurrentDirectory();

            //dump the generated code
            using (FileStream fs = File.OpenWrite(Path.Combine(outputDir, name + ".lol.cs")))
            {
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(code);
                writer.Flush();
                writer.Close();
            }

            Compiler.CompilerParameters compilerParams = new Compiler.CompilerParameters();
            compilerParams.GenerateInMemory = inMemory;
            if (!inMemory)
            {
                compilerParams.OutputAssembly = Path.Combine(outputDir, name + ".exe");
            }


            compilerParams.ReferencedAssemblies.Add("TT.dll");
            compilerParams.TreatWarningsAsErrors = false;
            compilerParams.CompilerOptions = "/optimize";
            //compilerParams.MainClass = "x";
            compilerParams.GenerateExecutable = false;
            compilerParams.GenerateInMemory = true;

            var compilerOptions = new Dictionary<string, string> { { "CompilerVersion", "v3.5" } };
            CSharpCodeProvider provider = new CSharpCodeProvider(compilerOptions);
            Compiler.CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, code);

            if (results.Errors.HasErrors)
            {
                foreach (var error in results.Errors)
                {
                    Debug.WriteLine(error.ToString());
                }

                throw new Exception("Internal compiler error."); //this means a bug in our compiler, not the user's code
            }

            return results.CompiledAssembly;
        }