Exemple #1
0
        static private AssemblyResults CompileAssembly(String code)
        {
            try
            {
                StreamWriter w = new StreamWriter("C:\\Program Files\\ILNET\\ILNET Server\\Solution\\project.cs");
                w.Write(code);
                w.Close();

                Dictionary <string, string> providerOptions = new Dictionary <string, string>();
                providerOptions.Add("CompilerVersion", "v4.0");
                CodeDomProvider codeProvider = new CSharpCodeProvider(providerOptions);

                ICodeCompiler      icc = codeProvider.CreateCompiler();
                CompilerParameters cp  = new CompilerParameters()
                {
                    GenerateInMemory        = false,
                    IncludeDebugInformation = Debugger.IsAttached,
                    TreatWarningsAsErrors   = false,
                    WarningLevel            = 0,
                    GenerateExecutable      = false,
                    CompilerOptions         = "/optimize+"
                };

                cp.ReferencedAssemblies.Add("System.dll");
                cp.ReferencedAssemblies.Add("System.Core.dll");
                cp.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
                cp.ReferencedAssemblies.Add("System.Data.dll");
                cp.ReferencedAssemblies.Add("System.Drawing.dll");
                cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                cp.ReferencedAssemblies.Add(FullReference("System.Xml.dll"));
                cp.ReferencedAssemblies.Add(FullReference("System.Xml.Linq.dll"));
                //cp.ReferencedAssemblies.Add(FullReference("MySql.Data.dll"));
                cp.ReferencedAssemblies.Add(FullReference("Common.dll"));
                AssemblyResults asm = Eval.CreateAssembly(icc, code, cp, new CSharpLanguage());
                return(asm);
            }
            catch (CompilationException ce)
            {
                foreach (CompilerError error in ce.Errors)
                {
                    if (error.IsWarning)
                    {
                        continue;
                    }
                    throw new Exception(Utils.Extract(code, "fname", code.NthIndexOf("\n", error.Line)).Substring(3), ce);
                }
                throw new Exception("", ce);
            }
        }