public static CompilationResult GetAssemblyFromCompilation(
            IAssemblyLoadContext loader,
            Compilation compilation)
        {
            EmitResult result;

            using (var ms = new MemoryStream())
            {
                using (var pdb = new MemoryStream())
                {
                    if (PlatformHelper.IsMono)
                    {
                        result = compilation.Emit(ms, pdbStream: null);
                    }
                    else
                    {
                        result = compilation.Emit(ms, pdbStream: pdb);
                    }

                    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;
                    if (PlatformHelper.IsMono)
                    {
                        assembly = loader.LoadStream(ms, assemblySymbols: null);
                    }
                    else
                    {
                        pdb.Seek(0, SeekOrigin.Begin);
                        assembly = loader.LoadStream(ms, pdb);
                    }

                    return(CompilationResult.FromAssembly(assembly));
                }
            }
        }
Exemple #2
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));
            }
        }