Esempio n. 1
0
        CompileFromFile(
            String InSourcePath, ExecutableType InExecType,
            GenerateAssemblyIn InGenerateIn,
            CompilerVersion CompilerVersion)
        {
            List <string>     addnReferencedAssemblies = new List <string>();
            AcCompilerResults results = Compile_Actual(
                InSourcePath, null, InExecType, InGenerateIn,
                addnReferencedAssemblies,
                CompilerVersion);

            return(results);
        }
Esempio n. 2
0
        CompileFromSource(
            string InSourceLines, ExecutableType InExecType,
            GenerateAssemblyIn InGenerateIn,
            List <string> InAddnReferencedAssemblies,
            CompilerVersion CompilerVersion)
        {
            AcCompilerResults results = Compile_Actual(
                null, InSourceLines, InExecType, InGenerateIn,
                InAddnReferencedAssemblies,
                CompilerVersion);

            return(results);
        }
Esempio n. 3
0
        Compile_Actual(
            string InSourcePath,
            string InSourceLines,
            ExecutableType InExecType,
            GenerateAssemblyIn InGenerateIn,
            List <string> InAddnReferencedAssemblies,
            CompilerVersion CompilerVersion
            )
        {
            System.Reflection.Assembly assem = null;
            List <string>   results          = new List <string>();
            CodeDomProvider provider         = null;
            // Microsoft.CSharp.CSharpCodeProvider provider = null;
            string               exeName = null;
            WipCompilerResults   wipCr   = new WipCompilerResults();
            AcCompilerParameters cp      = new AcCompilerParameters();

            // compile from lines in a source array or source file
            if (InSourceLines != null)
            {
                cp.SourceLines = InSourceLines;
            }
            else
            {
                cp.SourceFileInfo = new FileInfo(InSourcePath);
            }

            // Select the code provider based on the input file extension.
            if (cp.SourceLines != null)
            {
                Dictionary <string, string> provOpts =
                    new Dictionary <string, string>()
                {
                    { "CompilerVersion", "v3.5" }
                };
                provider = new Microsoft.CSharp.CSharpCodeProvider(provOpts);
//        provider = CodeDomProvider.CreateProvider("CSharp");
            }
            else if (cp.SourceFileInfo.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
            {
                Dictionary <string, string> provOpts =
                    new Dictionary <string, string>()
                {
                    { "CompilerVersion", "v3.5" }
                };
                provider = new Microsoft.CSharp.CSharpCodeProvider(provOpts);
//        provider = CodeDomProvider.CreateProvider("CSharp");
            }
            else
            {
                results.Add("Source file must have a .cs or .vb extension");
            }

            if (provider != null)
            {
                //  type of executable: Windows, Console, ClassLibrary
                cp.ExecutableType     = InExecType;
                cp.GenerateAssemblyIn = InGenerateIn;

                // setup name and path of the executable file to be created.
                if (cp.GenerateAssemblyIn == GenerateAssemblyIn.File)
                {
                    if (cp.ExecutableType == ExecutableType.ClassLibrary)
                    {
                        exeName = AcFilePath.NameSansExtension(cp.SourceFileInfo.Name) + ".dll";
                    }
                    else
                    {
                        exeName = AcFilePath.NameSansExtension(cp.SourceFileInfo.Name) + ".exe";
                    }
                    string exePath = AcFilePath.AddTo(cp.SourceFileInfo.DirectoryName, exeName);
                    cp.OutputAssembly = exePath;
                }

                // Set whether to treat all warnings as errors.
                cp.TreatWarningsAsErrors = false;
                cp.ReferencedAssemblies.Add("System.Dll");
                cp.ReferencedAssemblies.Add("System.Drawing.Dll");
                cp.ReferencedAssemblies.Add("System.Xml.Dll");
                cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");

                // additional assemblies
                foreach (string s1 in InAddnReferencedAssemblies)
                {
                    cp.ReferencedAssemblies.Add(s1);
                }

                // Invoke compilation of the source file.
                CompilerResults cr = null;
                if (cp.SourceLines != null)
                {
                    cr = provider.CompileAssemblyFromSource(cp, cp.SourceLines);
                }
                else
                {
                    cr = provider.CompileAssemblyFromFile(cp, cp.SourceFileInfo.FullName);
                }
                wipCr.SetCompilerResults(cr);
                wipCr.SetCompilerParameters(cp);

                // completion message
                results.Add(wipCr.CompletionMessage);

                if (wipCr.Errors.Count > 0)
                {
                    foreach (CompilerError ce in wipCr.Errors)
                    {
                        results.Add("   " + ce.ToString());
                    }
                }

                // Return the results of the compilation.
                if (wipCr.Errors.Count > 0)
                {
                    assem = null;
                }

                else
                {
                    assem = wipCr.CompiledAssembly;
                }
            }
            return(wipCr);
        }