示例#1
0
        private void ProcessResult(ReflectionEmitResult result)
        {
            _errorList.Items.Clear();

            if (!result.Success)
            {
                foreach (var diagnostic in result.Diagnostics)
                {
                    if (!diagnostic.Location.IsInSource)
                    {
                        throw new NotSupportedException("Only support source file locations.");
                    }

                    var itemType    = GetItemType(diagnostic.Info.Severity);
                    var description = diagnostic.Info.GetMessage();
                    var lineSpan    = diagnostic.Location.GetLineSpan(false);

                    _errorList.AddItem(itemType, description,
                                       lineSpan.Path,
                                       lineSpan.StartLinePosition.Line,
                                       lineSpan.StartLinePosition.Character,
                                       () =>
                    {
                        var openDocumentResult = new OpenDocumentResult(lineSpan.Path);
                        IoC.BuildUp(openDocumentResult);
                        openDocumentResult.Execute(null);
                    });
                }
            }

            if (result.IsUncollectible)
            {
                _errorList.AddItem(ErrorListItemType.Message, "The compiled assembly is not garbage collectible.");
            }
        }
示例#2
0
        private void ProcessResult(ReflectionEmitResult result)
        {
            _errorList.Items.Clear();

            if (!result.Success)
            {
                foreach (var diagnostic in result.Diagnostics)
                {
                    if (!diagnostic.Location.IsInSource)
                        throw new NotSupportedException("Only support source file locations.");

                    var itemType = GetItemType(diagnostic.Info.Severity);
                    var description = diagnostic.Info.GetMessage();
                    var lineSpan = diagnostic.Location.GetLineSpan(false);

                    _errorList.AddItem(itemType, description,
                        lineSpan.Path,
                        lineSpan.StartLinePosition.Line,
                        lineSpan.StartLinePosition.Character,
                        () =>
                        {
                            var openDocumentResult = new OpenDocumentResult(lineSpan.Path);
                            IoC.BuildUp(openDocumentResult);
                            openDocumentResult.Execute(null);
                        });
                }
            }

            if (result.IsUncollectible)
                _errorList.AddItem(ErrorListItemType.Message, "The compiled assembly is not garbage collectible.");
        }
示例#3
0
 internal static Submission FromCompilation(
     Type delegateType,
     Compilation compilation,
     ReflectionEmitResult emitResult,
     CommonSubmission previousInteraction)
 {
     Imports imports = ((SourceNamespaceSymbol)compilation.SourceModule.GlobalNamespace).GetBoundImports().SingleOrDefault() ?? Imports.Empty;
     var factory = (emitResult != null) ? Delegate.CreateDelegate(delegateType, emitResult.EntryPoint) : null;
     return new Submission(previousInteraction, compilation.ScriptClass, imports, factory);
 }
示例#4
0
        internal static Submission FromCompilation(
            Type delegateType,
            Compilation compilation,
            ReflectionEmitResult emitResult,
            CommonSubmission previousInteraction)
        {
            Imports imports = ((SourceNamespaceSymbol)compilation.SourceModule.GlobalNamespace).GetBoundImports().SingleOrDefault() ?? Imports.Empty;
            var     factory = (emitResult != null) ? Delegate.CreateDelegate(delegateType, emitResult.EntryPoint) : null;

            return(new Submission(previousInteraction, compilation.ScriptClass, imports, factory));
        }
示例#5
0
        private CompilationResult CompileCSharp(string fullClassName, IFileInfo file, bool success, List <CompilationMessage> messages, CodeCompileUnit codeCompileUnit)
        {
            // Generate code text
            var code = new StringBuilder();

            using (var provider = new CSharpCodeProvider())
            {
                using (var writer = new StringWriter(code))
                {
                    provider.GenerateCodeFromCompileUnit(codeCompileUnit, writer, new CodeGeneratorOptions());
                }
            }

            // Parse
            SyntaxTree tree = SyntaxTree.ParseText/*.ParseCompilationUnit*/ (code.ToString(), "__Generated.cs");

            // Create a compilation
            CSCompilation comp = CSCompilation.Create(
                "Compiled",
                new CompilationOptions(OutputKind.DynamicallyLinkedLibrary),
                syntaxTrees: new[] { tree },
                references: new[]
            {
                new MetadataFileReference(typeof(object).Assembly.Location),
                new MetadataFileReference(typeof(Enumerable).Assembly.Location),
                new MetadataFileReference(typeof(PageBase).Assembly.Location)
            });

            // Emit to a collectable assembly
            AssemblyBuilder asm = AssemblyBuilder.DefineDynamicAssembly(
                new AssemblyName("Razor_" + Guid.NewGuid().ToString("N")), AssemblyBuilderAccess.RunAndCollect);
            ModuleBuilder        mod    = asm.DefineDynamicModule("RazorCompilation");
            ReflectionEmitResult result = comp.Emit(mod);

            // Extract the type
            Type typ = null;

            if (result.Success)
            {
                typ = asm.GetType(fullClassName);
            }
            else
            {
                foreach (var diagnostic in result.Diagnostics)
                {
                    FileLinePositionSpan span         = diagnostic.Location.GetLineSpan(true);
                    LinePosition         linePosition = span.StartLinePosition;
                    messages.Add(new CompilationMessage(
                                     SeverityMap[diagnostic.Info.Severity],
                                     diagnostic.Info.GetMessage(),
                                     new FileLocation(
                                         span.Path,
                                         linePosition.Line,
                                         linePosition.Character,
                                         String.Equals(span.Path, "__Generated.cs", StringComparison.OrdinalIgnoreCase))));
                }
            }

            // Create a compilation result
            if (success && result.Success)
            {
                return(CompilationResult.Successful(code.ToString(), typ, messages));
            }
            return(CompilationResult.Failed(code.ToString(), messages));
        }