Esempio n. 1
0
        /// <summary>
        /// compile source code
        /// </summary>
        /// <returns>whether compile is ok</returns>
        public bool Compile()
        {
            bool bolResult = false;

            bolHasError                 = false;
            strCompilerOutput           = null;
            myCompilerErrors            = new CompilerErrorCollection();
            myResultAssembly            = null;
            this.bsResultAssemblyBinary = null;

            // add referenced assemblys information
            this.CompilerParameters.ReferencedAssemblies.Clear();
            foreach (string asm in this.ReferenceAssemblies)
            {
                this.CompilerParameters.ReferencedAssemblies.Add(asm);
            }

            // check domain
            ResolveEventHandler ResolveHandler = new ResolveEventHandler(myAppDomain_AssemblyResolve);

            if (myAppDomain != null)
            {
                myAppDomain.AssemblyResolve += ResolveHandler;
            }
            try
            {
                // compile
                this.CompilerParameters.GenerateExecutable      = false;
                this.CompilerParameters.GenerateInMemory        = false;
                this.CompilerParameters.IncludeDebugInformation = false;
                strRuntimeAssemblyFileName = this.AssemblyFileName;
                if (strRuntimeAssemblyFileName == null ||
                    strRuntimeAssemblyFileName.Trim().Length == 0)
                {
                    strRuntimeAssemblyFileName = System.IO.Path.GetTempFileName() + ".dll";
                }
                this.CompilerParameters.OutputAssembly = strRuntimeAssemblyFileName;

                if (this.CompilerImports != null && this.CompilerImports.Count > 0)
                {
                    System.Text.StringBuilder opt = new System.Text.StringBuilder();
                    foreach (string import in this.CompilerImports)
                    {
                        if (opt.Length > 0)
                        {
                            opt.Append(",");
                        }
                        opt.Append(import.Trim());
                    }
                    opt.Insert(0, " /imports:");
                    this.CompilerParameters.CompilerOptions = opt.ToString();
                }//if

                if (this.bolOutputDebug)
                {
                    System.Diagnostics.Debug.WriteLine(
                        ScriptStrings.StartDynamicCompile + "\r\n" + strSourceCode);
                    foreach (string dll in this.CompilerParameters.ReferencedAssemblies)
                    {
                        System.Diagnostics.Debug.WriteLine(ScriptStrings.Reference + ":" + dll);
                    }
                }
                CompilerResults result = null;
                if (this.Language == CompilerLanguage.VB)
                {
                    Microsoft.VisualBasic.VBCodeProvider provider
                        = new Microsoft.VisualBasic.VBCodeProvider();

                    result = provider.CompileAssemblyFromSource(
                        this.CompilerParameters,
                        strSourceCode);

                    provider.Dispose();
                }
                else if (this.Language == CompilerLanguage.CSharp)
                {
                    Microsoft.CSharp.CSharpCodeProvider provider
                        = new Microsoft.CSharp.CSharpCodeProvider();

                    result = provider.CompileAssemblyFromSource(
                        this.CompilerParameters,
                        strSourceCode);

                    provider.Dispose();
                }
                else
                {
                    throw new Exception(string.Format(
                                            ScriptStrings.NotSupportLanguage_Language, this.Language));
                }
                System.Text.StringBuilder myOutput = new System.Text.StringBuilder();
                foreach (string line in result.Output)
                {
                    myOutput.Append("\r\n" + line);
                }
                this.strCompilerOutput = myOutput.ToString();
                if (this.OutputDebug)
                {
                    if (this.strCompilerOutput.Length > 0)
                    {
                        System.Diagnostics.Debug.WriteLine(
                            ScriptStrings.DymamicCompilerResult + strCompilerOutput);
                    }
                }
                myCompilerErrors = result.Errors;
                if (result.Errors.HasErrors)
                {
                    bolResult   = false;
                    bolHasError = true;
                }
                else
                {
                    if (this.AutoLoadResultAssembly)
                    {
                        if (this.CompilerParameters.GenerateInMemory)
                        {
                            this.myResultAssembly = result.CompiledAssembly;
                        }
                        else
                        {
                            bsResultAssemblyBinary = null;
                            using (System.IO.FileStream stream = new System.IO.FileStream(
                                       this.CompilerParameters.OutputAssembly,
                                       System.IO.FileMode.Open,
                                       System.IO.FileAccess.Read))
                            {
                                bsResultAssemblyBinary = new byte[stream.Length];
                                stream.Read(
                                    bsResultAssemblyBinary,
                                    0,
                                    bsResultAssemblyBinary.Length);
                            }
                            this.myResultAssembly = myAppDomain.Load(
                                bsResultAssemblyBinary,
                                null,
                                this.CompilerParameters.Evidence);
                        }
                    }
                    bolResult = true;
                }
                if (this.PreserveAssemblyFile == false)
                {
                    if (System.IO.File.Exists(strRuntimeAssemblyFileName))
                    {
                        System.IO.File.Delete(strRuntimeAssemblyFileName);
                    }
                }
            }//if
            catch (Exception ext)
            {
                System.Diagnostics.Debug.WriteLine(
                    string.Format(ScriptStrings.CompileError_Message, ext.Message));
                if (bolThrowException)
                {
                    throw ext;
                }
            }
            if (myAppDomain != null)
            {
                myAppDomain.AssemblyResolve -= ResolveHandler;
            }
            return(bolResult);
        }