Ejemplo n.º 1
0
        private static Assembly CompileToAssembly(WebPageRazorHost host, SyntaxTree syntaxTree)
        {
            var compilation = CSharpCompilation.Create(
                "RoslynRazor", // Note: using a fixed assembly name, which doesn't matter as long as we don't expect cross references of generated assemblies
                new[] { syntaxTree },
                BuildManager.GetReferencedAssemblies().OfType <Assembly>().Select(ResolveReference),
                new CSharpCompilationOptions(
                    outputKind: OutputKind.DynamicallyLinkedLibrary,
                    assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default,
                    optimizationLevel: host.DefaultDebugCompilation ? OptimizationLevel.Debug : OptimizationLevel.Release));

            compilation = Hacks.MakeValueTuplesWorkWhenRunningOn47RuntimeAndTargetingNet45Plus(compilation);

            using (var dllStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    EmitResult emitResult = compilation.Emit(dllStream, pdbStream);

                    if (!emitResult.Success)
                    {
                        Diagnostic   diagnostic   = emitResult.Diagnostics.First(x => x.Severity == DiagnosticSeverity.Error);
                        string       message      = diagnostic.ToString();
                        LinePosition linePosition = diagnostic.Location.GetMappedLineSpan().StartLinePosition;

                        throw new HttpParseException(message, null, host.VirtualPath, null, linePosition.Line + 1);
                    }

                    return(Assembly.Load(dllStream.GetBuffer(), pdbStream.GetBuffer()));
                }
        }
        private static Assembly CompileToAssembly(WebPageRazorHost host, SyntaxTree syntaxTree, ICollection <ICompileModule> compilationModules)
        {
            var strArgs = new List <string>();

            strArgs.Add("/target:library");
            strArgs.Add(host.DefaultDebugCompilation ? "/o-" : "/o+");
            strArgs.Add(host.DefaultDebugCompilation ? "/debug+" : "/debug-");

            var cscArgs = CSharpCommandLineParser.Default.Parse(strArgs, null, null);

            var compilation = CSharpCompilation.Create(
                "RoslynRazor", // Note: using a fixed assembly name, which doesn't matter as long as we don't expect cross references of generated assemblies
                new[] { syntaxTree },
                BuildManager.GetReferencedAssemblies().OfType <Assembly>().Select(ResolveReference),
                cscArgs.CompilationOptions.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default));

            compilation = Hacks.MakeValueTuplesWorkWhenRunningOn47RuntimeAndTargetingNet45Plus(compilation);
            var diagnostics = new List <Diagnostic>();
            var context     = new CompileContext(compilationModules);

            context.Before(new BeforeCompileContext
            {
                Arguments   = cscArgs,
                Compilation = compilation,
                Diagnostics = diagnostics,
            });
            compilation = context.BeforeCompileContext.Compilation;

            using (var dllStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    EmitResult emitResult = compilation.Emit(dllStream, pdbStream);
                    diagnostics.AddRange(emitResult.Diagnostics);

                    if (!emitResult.Success)
                    {
                        Diagnostic   diagnostic   = diagnostics.First(x => x.Severity == DiagnosticSeverity.Error);
                        string       message      = diagnostic.ToString();
                        LinePosition linePosition = diagnostic.Location.GetMappedLineSpan().StartLinePosition;

                        throw new HttpParseException(message, null, host.VirtualPath, null, linePosition.Line + 1);
                    }

                    context.After(new AfterCompileContext
                    {
                        Arguments      = context.BeforeCompileContext.Arguments,
                        AssemblyStream = dllStream,
                        Compilation    = compilation,
                        Diagnostics    = diagnostics,
                        SymbolStream   = pdbStream,
                        XmlDocStream   = null,
                    });

                    return(Assembly.Load(dllStream.GetBuffer(), pdbStream.GetBuffer()));
                }
        }