当前位置: 首页>>代码示例>>C#>>正文


C# Compilation.GetDiagnostics方法代码示例

本文整理汇总了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);
     }
 }
开发者ID:gregoryyoung,项目名称:later,代码行数:9,代码来源:DiagnosticsFormatter.cs

示例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;
        }
开发者ID:hcn0843,项目名称:roslyn,代码行数:33,代码来源:CommandLineRunner.cs

示例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;
            }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:58,代码来源:AbstractMetadataAsSourceTests.TestContext.cs

示例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();
            }
开发者ID:jjrdk,项目名称:CqrsMessagingTools,代码行数:23,代码来源:ServicesTestFixture.cs

示例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();
        }
开发者ID:modulexcite,项目名称:PublicApiAnalyzer,代码行数:40,代码来源:FixAllContextHelper.cs

示例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;
        }
开发者ID:pranavr93,项目名称:MDClassDiagram,代码行数:66,代码来源:RoslynRun.cs


注:本文中的Compilation.GetDiagnostics方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。