Example #1
0
        private void VisitClass(SyntaxNodeAnalysisContext ctx)
        {
            var node = ctx.Node as ClassDeclarationSyntax;

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

            var classHasAuthAnnotation  = false;
            var classHasCacheAnnotation = false;

            AnalyzerUtil.ForEachAnnotation(node.AttributeLists,
                                           delegate(string Name, AttributeSyntax att) {
                if (Name == "Authorize")
                {
                    classHasAuthAnnotation = true;
                }
                else if (Name == "OutputCache")
                {
                    classHasCacheAnnotation = true;
                }
            }
                                           );

            foreach (MemberDeclarationSyntax member in node.Members)
            {
                var method = member as MethodDeclarationSyntax;
                if (method == null)
                {
                    continue;
                }

                var methodHasAuthAnnotation  = false;
                var methodHasCacheAnnotation = false;
                AnalyzerUtil.ForEachAnnotation(method.AttributeLists,
                                               delegate(string Name, 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()));
                }
            }
        }