本文整理汇总了C#中Compilation.GetDiagnostics方法的典型用法代码示例。如果您正苦于以下问题:C# Compilation.GetDiagnostics方法的具体用法?C# Compilation.GetDiagnostics怎么用?C# Compilation.GetDiagnostics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Compilation
的用法示例。
在下文中一共展示了Compilation.GetDiagnostics方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDiagnosticsFrom
public static void GetDiagnosticsFrom(Compilation compilation)
{
if (compilation.GetDiagnostics().Any())
{
var errors = compilation.GetDiagnostics().ToList();
foreach (var diag in errors)
Console.WriteLine(diag);
}
}
示例2: TryBuildAndRun
private bool TryBuildAndRun(Script<object> newScript, object globals, ref ScriptState<object> state, out Compilation newCompilation, CancellationToken cancellationToken)
{
newCompilation = newScript.GetCompilation();
try
{
newScript.Build(cancellationToken);
// display warnings:
DisplayDiagnostics(newCompilation.GetDiagnostics(cancellationToken).Where(d => d.Severity == DiagnosticSeverity.Warning));
}
catch (CompilationErrorException e)
{
DisplayDiagnostics(e.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error || d.Severity == DiagnosticSeverity.Warning));
return false;
}
try
{
var task = (state == null) ?
newScript.RunAsync(globals, cancellationToken) :
newScript.ContinueAsync(state, cancellationToken);
state = task.GetAwaiter().GetResult();
}
catch (Exception e)
{
DisplayException(e);
return false;
}
return true;
}
示例3: ResolveSymbolAsync
public async Task<ISymbol> ResolveSymbolAsync(string symbolMetadataName, Compilation compilation = null)
{
if (compilation == null)
{
compilation = await this.DefaultProject.GetCompilationAsync();
var diagnostics = compilation.GetDiagnostics().ToArray();
Assert.Equal(0, diagnostics.Length);
}
foreach (var reference in compilation.References)
{
var assemblySymbol = (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(reference);
var namedTypeSymbol = assemblySymbol.GetTypeByMetadataName(symbolMetadataName);
if (namedTypeSymbol != null)
{
return namedTypeSymbol;
}
else
{
// The symbol name could possibly be referring to the member of a named
// type. Parse the member symbol name.
var lastDotIndex = symbolMetadataName.LastIndexOf('.');
if (lastDotIndex < 0)
{
// The symbol name is not a member name and the named type was not found
// in this assembly
continue;
}
// The member symbol name itself could contain a dot (e.g. '.ctor'), so make
// sure we don't cut that off
while (lastDotIndex > 0 && symbolMetadataName[lastDotIndex - 1] == '.')
{
--lastDotIndex;
}
var memberSymbolName = symbolMetadataName.Substring(lastDotIndex + 1);
var namedTypeName = symbolMetadataName.Substring(0, lastDotIndex);
namedTypeSymbol = assemblySymbol.GetTypeByMetadataName(namedTypeName);
if (namedTypeSymbol != null)
{
var memberSymbol = namedTypeSymbol.GetMembers()
.Where(member => member.MetadataName == memberSymbolName)
.FirstOrDefault();
if (memberSymbol != null)
{
return memberSymbol;
}
}
}
}
return null;
}
示例4: given_a_compiled_application
public given_a_compiled_application()
{
//var resourceType = typeof (Resources);
//var assemblies = resourceType.GetProperties(BindingFlags.Static | BindingFlags.Public)
// .Select(y => y.GetValue(null, null))
// .OfType<byte[]>()
// .ToList();
//Assert.NotEmpty(assemblies);
//Assert.True(assemblies.Count == 7); // # of assemblies in resource file
AppCompilation = CSharpCompilation.Create("test")
.AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(infraCode))
.AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(code))
.AddReferences(new MetadataFileReference(typeof(object).Assembly.Location))
.AddReferences(new MetadataFileReference(typeof(IEnumerable<>).Assembly.Location));
// .AddReferences(assemblies.Select(x => new AssemblyBytesReference(x)));
var diag = AppCompilation.GetDiagnostics().Where(d => d.Severity >= DiagnosticSeverity.Warning);
Assert.Empty(diag);
sut = new ProcessAnalysisService();
}
示例5: GetAllDiagnosticsAsync
public static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Compilation compilation, CompilationWithAnalyzers compilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer> analyzers, IEnumerable<Document> documents, bool includeCompilerDiagnostics, CancellationToken cancellationToken)
{
if (GetAnalyzerSyntaxDiagnosticsAsync == null || GetAnalyzerSemanticDiagnosticsAsync == null)
{
// In everything except Roslyn 1.1, we use GetAllDiagnosticsAsync and return the result.
var allDiagnostics = await compilationWithAnalyzers.GetAllDiagnosticsAsync().ConfigureAwait(false);
return allDiagnostics;
}
compilationWithAnalyzers.Compilation.GetDeclarationDiagnostics(cancellationToken);
// Note that the following loop to obtain syntax and semantic diagnostics for each document cannot operate
// on parallel due to our use of a single CompilationWithAnalyzers instance.
var diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
foreach (var document in documents)
{
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var syntaxDiagnostics = await GetAnalyzerSyntaxDiagnosticsAsync(compilationWithAnalyzers, syntaxTree, cancellationToken).ConfigureAwait(false);
diagnostics.AddRange(syntaxDiagnostics);
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var semanticDiagnostics = await GetAnalyzerSemanticDiagnosticsAsync(compilationWithAnalyzers, semanticModel, default(TextSpan?), cancellationToken).ConfigureAwait(false);
diagnostics.AddRange(semanticDiagnostics);
}
foreach (var analyzer in analyzers)
{
diagnostics.AddRange(await GetAnalyzerCompilationDiagnosticsAsync(compilationWithAnalyzers, ImmutableArray.Create(analyzer), cancellationToken).ConfigureAwait(false));
}
if (includeCompilerDiagnostics)
{
// This is the special handling for cases where code fixes operate on warnings produced by the C#
// compiler, as opposed to being created by specific analyzers.
var compilerDiagnostics = compilation.GetDiagnostics(cancellationToken);
diagnostics.AddRange(compilerDiagnostics);
}
return diagnostics.ToImmutable();
}
示例6: ParseFiles
public static UMLClass ParseFiles(Compilation compilation)
{
foreach (var item in compilation.GetDiagnostics())
{
if (item.Severity == DiagnosticSeverity.Error)
{
Console.WriteLine("Code has compile time errors. Kindly fix them");
}
}
var syntaxTrees = compilation.SyntaxTrees;
foreach(var syntaxTree in syntaxTrees)
{
models.Add(compilation.GetSemanticModel(syntaxTree));
}
UMLClass uml = new UMLClass();
// Each file in the project
foreach (var st in syntaxTrees)
{
// Get semantic model for this file for getting info like class namespaces
model = compilation.GetSemanticModel(st);
//For each class in file
var AllClasses = st.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>();
foreach (var EachClass in AllClasses)
{
ClassNode c = GetClassNode(EachClass);
c.Namespace = model.GetDeclaredSymbol(EachClass).ToString();
c.FilePath = st.FilePath;
uml.ClassNodes.Add(c);
}
//For each struct in file
var AllStructs = st.GetRoot().DescendantNodes().OfType<StructDeclarationSyntax>();
foreach (var EachStruct in AllStructs)
{
StructNode snode = GetStructNode(EachStruct);
snode.Namespace = model.GetDeclaredSymbol(EachStruct).ToString();
snode.FilePath = st.FilePath;
uml.StructNodes.Add(snode);
}
// For reach interface in file
var AllInterfaces = st.GetRoot().DescendantNodes().OfType<InterfaceDeclarationSyntax>();
foreach (var EachInterface in AllInterfaces)
{
InterfaceNode inf = GetInterfaceNode(EachInterface);
inf.Namespace = model.GetDeclaredSymbol(EachInterface).ToString();
inf.FilePath = st.FilePath;
uml.InterfaceNodes.Add(inf);
}
//For each enum in file
var AllEnums = st.GetRoot().DescendantNodes().OfType<EnumDeclarationSyntax>();
foreach (var EachEnum in AllEnums)
{
EnumNode enode = GetEnumNode(EachEnum);
enode.Namespace = model.GetDeclaredSymbol(EachEnum).ToString();
enode.FilePath = st.FilePath;
uml.EnumNodes.Add(enode);
}
}
return uml;
}