C# roslyn分析仪发出警告,这些警告随后被删除

C# roslyn分析仪发出警告,这些警告随后被删除,c#,visual-studio-2015,roslyn,C#,Visual Studio 2015,Roslyn,我有一个基于分析器默认模板的分析器。 我的问题是,当执行完全重建时,一些(但不是全部)警告会出现在错误列表上 当我打开文件时,当分析器在打开的文件上重新执行时,警告开始消失最终所有警告都会消失 // <summary> // check that the compiler is in a build mode that enables documentation analysis. // it's not clear when this is off, but

我有一个基于分析器默认模板的分析器。 我的问题是,当执行完全重建时,一些(但不是全部)警告会出现在错误列表上

当我打开文件时,当分析器在打开的文件上重新执行时,警告开始消失最终所有警告都会消失

    // <summary>
    // check that the compiler is in a build mode that enables documentation analysis.
    // it's not clear when this is off, but command line builds, and full rebuilds
    // seem to have it turned off from time to time.
    // </summary>
    internal static bool IsDocumentationModeOn(this SyntaxNodeAnalysisContext context)
    {
        return context.Node.SyntaxTree?.Options.DocumentationMode 
               != DocumentationMode.None;
    }
我是否错误地注册了这些分析仪。理想情况下,我只希望在正确加载代码模型后执行它们

任何关于如何改进这一点的建议都将是非常好的

    public override void Initialize(AnalysisContext context)
    {
        context.RegisterSyntaxNodeAction(
            this.HandleClassDeclaration,
            SyntaxKind.ClassDeclaration);
    }
此代码分析器文档(在本例中)位于类声明上。 当Xml文档节点不存在时,它会报告诊断

    private void HandleClassDeclaration(SyntaxNodeAnalysisContext context)
    {
        // THE CHECK IN MY ANSWER BELOW GOES HERE...
        var declaration = (ClassDeclarationSyntax)context.Node;
        {
            var hasDocumentation = declaration.HasDocumentation();
            if (!hasDocumentation)
            {
                var diagnostic = Diagnostic.Create(this.Descriptor, declaration.Identifier.GetLocation());
                context.ReportDiagnostic(diagnostic);
            }
        }
    }
我正在使用此代码查找文档

    public static DocumentationCommentTriviaSyntax GetDocumentationCommentTriviaSyntax(this SyntaxNode node)
    {
        if (node == null)
        {
            return null;
        }

        foreach (var leadingTrivia in node.GetLeadingTrivia())
        {
            var structure = leadingTrivia.GetStructure() as DocumentationCommentTriviaSyntax;

            if (structure != null)
            {
                return structure;
            }
        }

        return null;
    }

对于任何试图分析Roslyn中文档的人来说,需要进行这一小小的检查

    // <summary>
    // check that the compiler is in a build mode that enables documentation analysis.
    // it's not clear when this is off, but command line builds, and full rebuilds
    // seem to have it turned off from time to time.
    // </summary>
    internal static bool IsDocumentationModeOn(this SyntaxNodeAnalysisContext context)
    {
        return context.Node.SyntaxTree?.Options.DocumentationMode 
               != DocumentationMode.None;
    }
//
//检查编译器是否处于启用文档分析的生成模式。
//现在还不清楚何时关闭,但命令行会生成,并进行完整的重建
//似乎不时地把它关掉。
// 
内部静态bool IsDocumentationModeOn(此SyntaxNodeAnalysisContext上下文)
{
返回context.Node.SyntaxTree?.Options.DocumentationMode
!=文档模式。无;
}

能否将错误列表切换到“仅生成”视图,并查看警告是否仍然存在

如果警告仍然存在,则表示警告是由命令行生成的。如果打开文档后警告消失,则表示live analysis认为没有问题。由于构建和实时分析之间的这种差异,这个问题可能会发生


如果是这样的话,那将是罗斯林的一个漏洞。(更具体地说,实时分析和命令行生成之间的编译选项中的错误-VS内部的生成也是命令行生成,选项略有不同)

这反映了项目文件中的“生成XML文档注释”选项。在维护遗留行为的情况下,如果您没有生成遗留行为,我们就不会对其进行解析,因为之前我们允许您使用错误的语法。我注意到,尽管选中此选项后误报的数量有所减少,但该行为仍然不太正确。我仍然会遇到这样的情况:如果进行完全重建,会显示错误警告。我一打开文件,它们就消失了。不再错误地发出警告。下面的支票解决了大部分问题。我认为最后几次是由于我的分析器/修复程序中的一个错误,其中ID不匹配。