Пример #1
0
        public static CompilerResults Compile(Language lang, string[] sourceFiles, string[] referencedAssemblies)
        {
            CodeDomProvider provider;

            Dictionary<string, string> args = new Dictionary<string, string>();

            Trace.WriteLine(".NET runtime version: " + Environment.Version.ToString(), "Runtime");
            Trace.WriteLine(".NET installed version: " + DotNetVersion.ToString(), "Runtime");
            if (Net35) {
                args.Add("CompilerVersion", "v3.5");
                Trace.WriteLine("Using v3.5 compiler", "Runtime");
            }

            switch (lang) {
                case Language.CSharp:
                    provider = new CSharpCodeProvider(args);
                    Debug.Assert(provider.FileExtension.ToLowerInvariant() == "cs");
                    break;

                case Language.VisualBasic:
                    provider = new VBCodeProvider(args);
                    Debug.Assert(provider.FileExtension.ToLowerInvariant() == "vb");
                    break;

                case Language.Boo:
                    return CompileBoo(sourceFiles, referencedAssemblies);

                default:
                    throw new ArgumentException("Invalid language.");
            }

            CompilerParameters options = new CompilerParameters();
            options.GenerateExecutable = false;
            options.GenerateInMemory = true;
            options.IncludeDebugInformation = true;
            options.WarningLevel = 3;

            options.ReferencedAssemblies.Add("System.dll");
            options.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            options.ReferencedAssemblies.Add("System.Drawing.dll");
            options.ReferencedAssemblies.Add("System.Xml.dll");
            options.ReferencedAssemblies.Add("System.Data.dll");
            options.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");

            if (Net35) {
                options.ReferencedAssemblies.Add("System.Core.dll");
                options.ReferencedAssemblies.Add("System.Xml.Linq.dll");
            }

            foreach (string reference in referencedAssemblies) {
                options.ReferencedAssemblies.Add(reference);
            }

            CompilerResults result = CompileLanguage(sourceFiles, provider, options);

            provider.Dispose();

            return result;
        }