Ejemplo n.º 1
0
        private static AssemblyLoadResult ReportCompilationError(EmitResult result)
        {
#if DESKTOP // TODO: Temporary due to CoreCLR and Desktop Roslyn being out of sync
            var formatter = DiagnosticFormatter.Instance;
#else
            var formatter = new DiagnosticFormatter();
#endif
            var errors = new List <string>(result.Diagnostics.Select(d => formatter.Format(d)));

            return(new AssemblyLoadResult(errors));
        }
Ejemplo n.º 2
0
        private AssemblyLoadResult CompileToDisk(string assemblyPath, string pdbPath, Compilation compilation, IList <ResourceDescription> resources)
        {
#if DESKTOP
            EmitResult result = compilation.Emit(assemblyPath, pdbPath, manifestResources: resources);
#else
            EmitResult result = compilation.Emit(assemblyPath);
#endif

            if (!result.Success)
            {
                // REVIEW: Emit seems to create the output assembly even if the build fails
                // follow up to see why this happens
                if (File.Exists(assemblyPath))
                {
                    File.Delete(assemblyPath);
                }

                return(ReportCompilationError(result));
            }

            return(new AssemblyLoadResult(Assembly.LoadFile(assemblyPath)));
        }
Ejemplo n.º 3
0
        private AssemblyLoadResult CompileInMemory(string name, Compilation compilation, IEnumerable <ResourceDescription> resources)
        {
            using (var pdbStream = new MemoryStream())
                using (var assemblyStream = new MemoryStream())
                {
#if DESKTOP
                    EmitResult result = compilation.Emit(assemblyStream, pdbStream: pdbStream, manifestResources: resources);
#else
                    EmitResult result = compilation.Emit(assemblyStream);
#endif

                    if (!result.Success)
                    {
                        return(ReportCompilationError(result));
                    }

                    var assemblyBytes = assemblyStream.ToArray();

#if DESKTOP
                    var pdbBytes = pdbStream.ToArray();
#endif

                    var compiled = new CompiledAssembly
                    {
#if DESKTOP
                        Assembly = Assembly.Load(assemblyBytes, pdbBytes),
#else
                        Assembly = Assembly.Load(assemblyBytes),
#endif
                        MetadataReference = compilation.ToMetadataReference()
                    };

                    _compiledAssemblies[name] = compiled;

                    return(new AssemblyLoadResult(compiled.Assembly));
                }
        }
Ejemplo n.º 4
0
        private static AssemblyLoadResult ReportCompilationError(EmitResult result)
        {
            #if DESKTOP // TODO: Temporary due to CoreCLR and Desktop Roslyn being out of sync
            var formatter = DiagnosticFormatter.Instance;
            #else
            var formatter = new DiagnosticFormatter();
            #endif
            var errors = new List<string>(result.Diagnostics.Select(d => formatter.Format(d)));

            return new AssemblyLoadResult(errors);
        }