コード例 #1
0
        private void VisitClassEx(SyntaxNodeAnalysisContext ctx)
        {
            var node = ctx.Node as Microsoft.CodeAnalysis.VisualBasic.Syntax.ClassBlockSyntax;

            if (node == null)
            { //Not the expected node type
                return;
            }

            var classHasAuthAnnotation  = false;
            var classHasCacheAnnotation = false;

            AnalyzerUtil.ForEachAnnotationEx(node.ClassStatement.AttributeLists,
                                             delegate(string Name, Microsoft.CodeAnalysis.VisualBasic.Syntax.AttributeSyntax att) {
                if (Name == "Authorize")
                {
                    classHasAuthAnnotation = true;
                }
                else if (Name == "OutputCache")
                {
                    classHasCacheAnnotation = true;
                }
            }
                                             );

            foreach (Microsoft.CodeAnalysis.VisualBasic.Syntax.StatementSyntax member in node.Members)
            {
                var method = member as Microsoft.CodeAnalysis.VisualBasic.Syntax.MethodBlockSyntax;
                if (method == null)
                {
                    continue;
                }

                var methodHasAuthAnnotation  = false;
                var methodHasCacheAnnotation = false;
                AnalyzerUtil.ForEachAnnotationEx(method.BlockStatement.AttributeLists,
                                                 delegate(string Name, Microsoft.CodeAnalysis.VisualBasic.Syntax.AttributeSyntax att)
                {
                    if (Name == "Authorize")
                    {
                        methodHasAuthAnnotation = true;
                    }
                    else if (Name == "OutputCache")
                    {
                        methodHasCacheAnnotation = true;
                    }
                }
                                                 );

                bool hasAuth  = classHasAuthAnnotation || methodHasAuthAnnotation;
                bool hasCache = classHasCacheAnnotation || methodHasCacheAnnotation;

                if (hasAuth && hasCache)
                {
                    ctx.ReportDiagnostic(Diagnostic.Create(Rule, method.GetLocation()));
                }
            }
        }