示例#1
0
        } // End Sub Test 
        
        
        // emit the compilation result into a byte array.
        // throw an exception with corresponding message
        // if there are errors
        private static byte[] EmitToArray
        (
            this Microsoft.CodeAnalysis.Compilation compilation
        )
        {
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                // emit result into a stream
                Microsoft.CodeAnalysis.Emit.EmitResult emitResult = compilation.Emit(stream);

                if (!emitResult.Success)
                {
                    // if not successful, throw an exception
                    Microsoft.CodeAnalysis.Diagnostic firstError =
                        emitResult
                            .Diagnostics
                            .FirstOrDefault
                            (
                                diagnostic =>
                                    diagnostic.Severity ==
                                    Microsoft.CodeAnalysis.DiagnosticSeverity.Error
                            );

                    throw new System.Exception(firstError?.GetMessage());
                }

                // get the byte array from a stream
                return stream.ToArray();
            } // End Using stream
            
        } // End Function EmitToArray 
示例#2
0
        public CompilationException(Microsoft.CodeAnalysis.Diagnostic diagnostic) : base(diagnostic.GetMessage())
        {
            var location = diagnostic.Location;

            if (location != Microsoft.CodeAnalysis.Location.None)
            {
                Line = location.GetLineSpan().StartLinePosition.Line;
            }
        }
示例#3
0
        public static FileAnalysisEntry FromDiagnostic(Microsoft.CodeAnalysis.Diagnostic diagnostic)
        {
            var lineSpan = diagnostic.Location.GetLineSpan();

            return(new FileAnalysisEntry
            {
                Id = diagnostic.Id,
                Severity = diagnostic.Severity.ToString(),
                Description = diagnostic.GetMessage(),
                From = new EntryLocation {
                    Line = lineSpan.StartLinePosition.Line, Character = lineSpan.StartLinePosition.Character
                },
                To = new EntryLocation {
                    Line = lineSpan.EndLinePosition.Line, Character = lineSpan.EndLinePosition.Character
                }
            });
        }
示例#4
0
        // https://gist.github.com/GeorgDangl/4a9982a3b520f056a9e890635b3695e0
        private static void ThrowExceptionIfCompilationFailure(Microsoft.CodeAnalysis.Emit.EmitResult result)
        {
            if (!result.Success)
            {
                System.Collections.Generic.List <Microsoft.CodeAnalysis.Diagnostic> compilationErrors =
                    result.Diagnostics.Where(diagnostic =>
                                             diagnostic.IsWarningAsError ||
                                             diagnostic.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
                    .ToList();

                if (compilationErrors.Any())
                {
                    Microsoft.CodeAnalysis.Diagnostic firstError = compilationErrors.First();
                    string errorNumber       = firstError.Id;
                    string errorDescription  = firstError.GetMessage();
                    string firstErrorMessage = $"{errorNumber}: {errorDescription};";
                    throw new System.Exception($"Compilation failed, first error is: {firstErrorMessage}");
                }
            }
        }
示例#5
0
 public Diagnostic(Context cx, Microsoft.CodeAnalysis.Diagnostic diag) : base(cx)
 {
     diagnostic = diag;
     TryPopulate();
 }