Пример #1
0
        /// <summary>
        /// Compiles the given compilation and returns the assembly.
        /// </summary>
        /// <param name="compilation">Compilation</param>
        /// <param name="outputKind">OutputKind</param>
        /// <returns>Assembly</returns>
        private Assembly ToAssembly(CodeAnalysis.Compilation compilation, OutputKind outputKind)
        {
            string assemblyFileName = null;

            if (outputKind == OutputKind.ConsoleApplication)
            {
                assemblyFileName = compilation.AssemblyName + ".exe";
            }
            else if (outputKind == OutputKind.DynamicallyLinkedLibrary)
            {
                assemblyFileName = compilation.AssemblyName + ".dll";
            }

            EmitResult emitResult = null;

            using (var ms = new MemoryStream())
            {
                emitResult = compilation.Emit(ms);
                if (emitResult.Success)
                {
                    var assembly = Assembly.Load(ms.GetBuffer());
                    return(assembly);
                }
            }

            Output.PrintLine("---");
            Output.PrintLine("Note: the errors below correspond to the intermediate C#-IR, " +
                             "which can be printed using /debug.");
            Output.PrintLine("---");

            var message = string.Join("\r\n", emitResult.Diagnostics);

            throw new ApplicationException(message);
        }
Пример #2
0
        /// <summary>
        /// Compiles the given compilation to a file.
        /// </summary>
        /// <param name="compilation">Compilation</param>
        /// <param name="outputKind">OutputKind</param>
        /// <param name="outputPath">OutputPath</param>
        /// <returns>Output</returns>
        private string ToFile(CodeAnalysis.Compilation compilation, OutputKind outputKind, string outputPath)
        {
            string assemblyFileName = null;

            if (outputKind == OutputKind.ConsoleApplication)
            {
                assemblyFileName = compilation.AssemblyName + ".exe";
            }
            else if (outputKind == OutputKind.DynamicallyLinkedLibrary)
            {
                assemblyFileName = compilation.AssemblyName + ".dll";
            }

            string outputDirectory;

            if (!this.CompilationContext.Configuration.OutputFilePath.Equals(""))
            {
                outputDirectory = this.CompilationContext.Configuration.OutputFilePath;
            }
            else
            {
                outputDirectory = Path.GetDirectoryName(outputPath);
            }

            this.OutputDirectoryMap.Add(compilation.AssemblyName, outputDirectory);

            string fileName    = outputDirectory + Path.DirectorySeparatorChar + assemblyFileName;
            string pdbFileName = outputDirectory + Path.DirectorySeparatorChar + compilation.AssemblyName + ".pdb";

            this.ProjectAssemblyPathMap.Add(compilation.AssemblyName, fileName);

            // Link external references.
            this.LinkExternalAssembliesToProject(compilation);

            EmitResult emitResult = null;

            using (FileStream outputFile = new FileStream(fileName, FileMode.Create, FileAccess.Write),
                   outputPdbFile = new FileStream(pdbFileName, FileMode.Create, FileAccess.Write))
            {
                emitResult = compilation.Emit(outputFile, outputPdbFile);
                if (emitResult.Success)
                {
                    Output.PrintLine("... Writing " + fileName);
                    return(fileName);
                }
            }

            Output.PrintLine("---");
            Output.PrintLine("Note: the errors below correspond to the intermediate C#-IR, " +
                             "which can be printed using /debug.");
            Output.PrintLine("---");

            var message = string.Join("\r\n", emitResult.Diagnostics);

            throw new ApplicationException(message);
        }
        public static AssemblyGenerationResult GetAssemblyFromCompilation(
            CodeAnalysis.Compilation compilation)
        {
            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms, pdbStream: null);

                if (!result.Success)
                {
                    var formatter     = new DiagnosticFormatter();
                    var errorMessages = result.Diagnostics
                                        .Where(IsError)
                                        .Select(d => formatter.Format(d));

                    return(AssemblyGenerationResult.FromErrorMessages(errorMessages));
                }

                ms.Seek(0, SeekOrigin.Begin);

                Assembly assembly;
                try
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        ms.CopyTo(memoryStream);
                        assembly = Assembly.Load(memoryStream.ToArray());
                    }
                }
                catch (Exception ex)
                {
                    var v = ex;
                    while (v.InnerException != null)
                    {
                        v = v.InnerException;
                    }
                    throw ex;
                }

                return(AssemblyGenerationResult.FromAssembly(assembly));
            }
        }
Пример #4
0
        public static CompilationResult GetAssemblyFromCompilation(
            ICodeGenAssemblyLoadContext loader,
            CodeAnalysis.Compilation compilation)
        {
            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms, pdbStream: null);

                if (!result.Success)
                {
                    var formatter     = new DiagnosticFormatter();
                    var errorMessages = result.Diagnostics
                                        .Where(IsError)
                                        .Select(d => formatter.Format(d));

                    return(CompilationResult.FromErrorMessages(errorMessages));
                }

                ms.Seek(0, SeekOrigin.Begin);

                Assembly assembly;
                try
                {
                    assembly = loader.LoadStream(ms, symbols: null);
                }
                catch (Exception ex)
                {
                    var v = ex;
                    while (v.InnerException != null)
                    {
                        v = v.InnerException;
                    }
                    throw ex;
                }

                return(CompilationResult.FromAssembly(assembly));
            }
        }
Пример #5
0
        /// <summary>
        /// Compiles the given compilation to a file.
        /// </summary>
        public string ToFile(CodeAnalysis.Compilation compilation, OutputKind outputKind,
                             string outputPath, bool printResults, bool buildDebugFile)
        {
            string assemblyFileName = null;

            if (outputKind == OutputKind.ConsoleApplication)
            {
                assemblyFileName = compilation.AssemblyName + ".exe";
            }
            else if (outputKind == OutputKind.DynamicallyLinkedLibrary)
            {
                assemblyFileName = compilation.AssemblyName + ".dll";
            }

            string outputDirectory;

            if (!string.IsNullOrEmpty(this.CompilationContext.Configuration.OutputFilePath))
            {
                outputDirectory = this.CompilationContext.Configuration.OutputFilePath;
            }
            else
            {
                outputDirectory = Path.GetDirectoryName(outputPath);
            }

            string fileName    = outputDirectory + Path.DirectorySeparatorChar + assemblyFileName;
            string pdbFileName = outputDirectory + Path.DirectorySeparatorChar + compilation.AssemblyName + ".pdb";

            this.OutputDirectoryMap?.Add(compilation.AssemblyName, outputDirectory);
            this.ProjectAssemblyPathMap?.Add(compilation.AssemblyName, fileName);

            EmitResult emitResult = null;

            using (FileStream outputFile = new FileStream(fileName, FileMode.Create, FileAccess.Write),
                   outputPdbFile = new FileStream(pdbFileName, FileMode.Create, FileAccess.Write))
            {
                if (buildDebugFile)
                {
                    emitResult = compilation.Emit(outputFile, outputPdbFile);
                }
                else
                {
                    emitResult = compilation.Emit(outputFile, null);
                }
            }

            if (emitResult.Success)
            {
                if (printResults)
                {
                    this.Logger.WriteLine("... Writing {0}", fileName);
                }

                return(fileName);
            }

            this.Logger.WriteLine("---");
            this.Logger.WriteLine("Note: the errors below correspond to the intermediate C#-IR, " +
                                  "which can be printed using /debug.");
            this.Logger.WriteLine("---");

            var message = string.Join("\r\n", emitResult.Diagnostics);

            throw new ApplicationException(message);
        }