當前位置: 首頁>>代碼示例>>C#>>正文


C# AnalysisContext類代碼示例

本文整理匯總了C#中AnalysisContext的典型用法代碼示例。如果您正苦於以下問題:C# AnalysisContext類的具體用法?C# AnalysisContext怎麽用?C# AnalysisContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


AnalysisContext類屬於命名空間,在下文中一共展示了AnalysisContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Initialize

        public override void Initialize(AnalysisContext analysisContext)
        {
            analysisContext.EnableConcurrentExecution();
            analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

            analysisContext.RegisterCompilationAction(AnalyzeCompilation);
        }
開發者ID:bkoelman,項目名稱:roslyn-analyzers,代碼行數:7,代碼來源:MarkAssembliesWithComVisible.cs

示例2: Initialize

 public override void Initialize(AnalysisContext context)
 {
     context.RegisterSyntaxNodeAction((syntaxContext) =>
     {
         syntaxContext.ReportDiagnostic(Diagnostic.Create(Rule, syntaxContext.Node.GetLocation()));
     }, SyntaxKind.ParamsKeyword);
 }
開發者ID:johnkoerner,項目名稱:AnalyzerSamples,代碼行數:7,代碼來源:DoNotUseParamsAnalyzer.cs

示例3: Initialize

 public override void Initialize(AnalysisContext context)
 {
     context.RegisterSyntaxNodeAction(
         (nodeContext) =>
         {
             Diagnostic diagnostic;
             if (TryGetDiagnostic(nodeContext, out diagnostic))
             {
                 nodeContext.ReportDiagnostic(diagnostic);
             }
         },
         new SyntaxKind[] {
             SyntaxKind.MethodDeclaration,
                       SyntaxKind.FieldDeclaration,
                       SyntaxKind.PropertyDeclaration,
                       SyntaxKind.IndexerDeclaration,
                       SyntaxKind.EventDeclaration,
                       SyntaxKind.ConstructorDeclaration,
                       SyntaxKind.OperatorDeclaration,
                       SyntaxKind.ClassDeclaration,
                       SyntaxKind.InterfaceDeclaration,
                       SyntaxKind.StructDeclaration,
                       SyntaxKind.EnumDeclaration,
                       SyntaxKind.DelegateDeclaration
         }
     );
 }
開發者ID:Kavignon,項目名稱:RefactoringEssentials,代碼行數:27,代碼來源:RedundantPrivateAnalyzer.cs

示例4: Initialize

        public override void Initialize(AnalysisContext context)
        {
            Arg.IsNotNull(() => context);

            // TODO: Consider registering other actions that act on syntax instead of or in addition to symbols
            context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
        }
開發者ID:jwendl,項目名稱:CipherCritic,代碼行數:7,代碼來源:XmlDocumentMissingAnalyzer.cs

示例5: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    if (c.SemanticModel.Compilation.IsTest() ||
                        c.SemanticModel.Compilation.Options.OutputKind != OutputKind.DynamicallyLinkedLibrary)
                    {
                        //this rule only makes sense in libraries
                        return;
                    }

                    var awaitExpression = (AwaitExpressionSyntax)c.Node;
                    var expression = awaitExpression.Expression;
                    if (expression == null)
                    {
                        return;
                    }

                    var taskType = c.SemanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
                    if (taskType == null)
                    {
                        return;
                    }

                    var type = c.SemanticModel.GetTypeInfo(expression).Type;
                    if (type != null &&
                        taskType.Equals(type))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, expression.GetLocation()));
                    }
                },
                SyntaxKind.AwaitExpression);
        }
開發者ID:ozgurkayaist,項目名稱:sonarlint-vs,代碼行數:34,代碼來源:TaskConfigureAwait.cs

示例6: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var methodCall = (InvocationExpressionSyntax) c.Node;
                    var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel);
                    var argumentMappings = methodCall.ArgumentList.Arguments.Select(argument =>
                        new ArgumentParameterMapping(argument,
                            methodParameterLookup.GetParameterSymbol(argument)))
                        .ToList();

                    var methodSymbol = methodParameterLookup.MethodSymbol;
                    if (methodSymbol == null)
                    {
                        return;
                    }

                    foreach (var argumentMapping in argumentMappings)
                    {
                        if (ArgumentHasDefaultValue(argumentMapping, c.SemanticModel))
                        {
                            var argument = argumentMapping.Argument;
                            var parameter = argumentMapping.Parameter;
                            c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation(), parameter.Name));
                        }
                    }
                },
                SyntaxKind.InvocationExpression);
        }
開發者ID:jakobehn,項目名稱:sonarlint-vs,代碼行數:30,代碼來源:RedundantArgument.cs

示例7: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.EnableConcurrentExecution();
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

            context.RegisterCompilationStartAction(compilationContext =>
            {
                var exportAttribute = compilationContext.Compilation.GetTypeByMetadataName("System.Composition.ExportAttribute");

                if (exportAttribute == null)
                {
                    // We don't need to check assemblies unless they're referencing both MEFv2, so we're done
                    return;
                }

                compilationContext.RegisterSymbolAction(symbolContext =>
                {
                    var namedType = (INamedTypeSymbol)symbolContext.Symbol;
                    var namedTypeAttributes = namedType.GetApplicableAttributes();

                    var exportAttributeApplication = namedTypeAttributes.FirstOrDefault(ad => ad.AttributeClass.DerivesFrom(exportAttribute));

                    if (exportAttributeApplication != null)
                    {
                        if (!namedTypeAttributes.Any(ad => ad.AttributeClass.Name == "SharedAttribute" &&
                                                           ad.AttributeClass.ContainingNamespace.Equals(exportAttribute.ContainingNamespace)))
                        {
                            // '{0}' is exported with MEFv2 and hence must be marked as Shared
                            symbolContext.ReportDiagnostic(Diagnostic.Create(Rule, exportAttributeApplication.ApplicationSyntaxReference.GetSyntax().GetLocation(), namedType.Name));
                        }
                    }
                }, SymbolKind.NamedType);
            });
        }
開發者ID:bkoelman,項目名稱:roslyn-analyzers,代碼行數:34,代碼來源:PartsExportedWithMEFv2MustBeMarkedAsShared.cs

示例8: Initialize

        public override void Initialize(AnalysisContext analysisContext)
        {
            analysisContext.RegisterOperationAction(saContext =>
            {
                var fieldInitializer = saContext.Operation as IFieldInitializer;

                // Diagnostics are reported on the last initialized field to retain the previous FxCop behavior
                var lastField = fieldInitializer.InitializedFields.LastOrDefault();
                var fieldInitializerValue = fieldInitializer.Value;
                if (fieldInitializerValue == null||
                    lastField.IsConst ||
                    lastField.GetResultantVisibility() == SymbolVisibility.Public ||!lastField.IsStatic || !lastField.IsReadOnly ||
                    !fieldInitializerValue.ConstantValue.HasValue)
                {
                    return;
                }

                var initializerValue = fieldInitializerValue.ConstantValue.Value;

                // Though null is const we dont fire the diagnostic to be FxCop Compact
                if (initializerValue != null)
                {
                    if (fieldInitializerValue.Type == saContext.Compilation.GetSpecialType(SpecialType.System_String) &&
                        ((string)initializerValue)?.Length == 0)
                    {
                        saContext.ReportDiagnostic(lastField.CreateDiagnostic(EmptyStringRule, lastField.Name));
                        return;
                    }

                    saContext.ReportDiagnostic(lastField.CreateDiagnostic(DefaultRule, lastField.Name, initializerValue));
                }
            },
            OperationKind.FieldInitializerAtDeclaration);
        }
開發者ID:Anniepoh,項目名稱:roslyn-analyzers,代碼行數:34,代碼來源:UseLiteralsWhereAppropriate.cs

示例9: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSymbolAction(
                c =>
                {
                    var symbol = c.Symbol as INamedTypeSymbol;
                    if (symbol == null ||
                        !symbol.IsAbstract ||
                        symbol.TypeKind != TypeKind.Class)
                    {
                        return;
                    }

                    if (AbstractClassShouldBeInterface(symbol))
                    {
                        ReportClass(symbol, MessageToInterface, c);
                        return;
                    }

                    if (AbstractClassShouldBeConcreteClass(symbol))
                    {
                        ReportClass(symbol, MessageToConcreteClass, c);
                        return;
                    }
                },
                SymbolKind.NamedType);
        }
開發者ID:andwi,項目名稱:sonarlint-vs,代碼行數:27,代碼來源:ClassShouldNotBeAbstract.cs

示例10: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var field = (FieldDeclarationSyntax)c.Node;

                    foreach (var variable in field.Declaration.Variables
                        .Where(v => v.Initializer != null))
                    {
                        var variableSymbol = c.SemanticModel.GetDeclaredSymbol(variable) as IFieldSymbol;
                        if (variableSymbol == null ||
                            variableSymbol.IsConst)
                        {
                            continue;
                        }

                        if (CheckDefaultExpressionInitializer(variable) ||
                            CheckReferenceTypeNullInitializer(variable, variableSymbol) ||
                            CheckValueTypeDefaultValueInitializer(variable, variableSymbol))
                        {
                            c.ReportDiagnostic(Diagnostic.Create(Rule, variable.Initializer.GetLocation(), variableSymbol.Name));
                            return;
                        }
                    }
                },
                SyntaxKind.FieldDeclaration);
        }
開發者ID:jakobehn,項目名稱:sonarlint-vs,代碼行數:28,代碼來源:FieldInitializedToDefault.cs

示例11: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var assignment = (AssignmentExpressionSyntax) c.Node;

                    if (IsInSubExpression(assignment) ||
                        IsInCondition(assignment))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, assignment.OperatorToken.GetLocation(),
                            assignment.Left.ToString()));
                    }
                },
                SyntaxKind.SimpleAssignmentExpression,
                SyntaxKind.AddAssignmentExpression,
                SyntaxKind.SubtractAssignmentExpression,
                SyntaxKind.MultiplyAssignmentExpression,
                SyntaxKind.DivideAssignmentExpression,
                SyntaxKind.ModuloAssignmentExpression,
                SyntaxKind.AndAssignmentExpression,
                SyntaxKind.ExclusiveOrAssignmentExpression,
                SyntaxKind.OrAssignmentExpression,
                SyntaxKind.LeftShiftAssignmentExpression,
                SyntaxKind.RightShiftAssignmentExpression);
        }
開發者ID:jango2015,項目名稱:sonarlint-vs,代碼行數:26,代碼來源:AssignmentInsideSubExpression.cs

示例12: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var lessThan = (BinaryExpressionSyntax) c.Node;
                    int constValue;
                    if (SillyBitwiseOperation.TryGetConstantIntValue(lessThan.Left, out constValue) &&
                        constValue == 0 &&
                        IsIndexOfCall(lessThan.Right, c.SemanticModel))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(lessThan.SyntaxTree,
                            TextSpan.FromBounds(lessThan.Left.SpanStart, lessThan.OperatorToken.Span.End))));
                    }
                },
                SyntaxKind.LessThanExpression);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var greaterThan = (BinaryExpressionSyntax)c.Node;
                    int constValue;
                    if (SillyBitwiseOperation.TryGetConstantIntValue(greaterThan.Right, out constValue) &&
                        constValue == 0 &&
                        IsIndexOfCall(greaterThan.Left, c.SemanticModel))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(greaterThan.SyntaxTree,
                            TextSpan.FromBounds(greaterThan.OperatorToken.SpanStart, greaterThan.Right.Span.End))));
                    }
                },
                SyntaxKind.GreaterThanExpression);
        }
開發者ID:roel4ez,項目名稱:sonarlint-vs,代碼行數:32,代碼來源:IndexOfCheckAgainstZero.cs

示例13: Initialize

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterCodeBlockStartAction<SyntaxKind>(startCodeBlockContext =>
            {
                // We only care about method bodies.
                if (startCodeBlockContext.OwningSymbol.Kind != SymbolKind.Method)
                {
                    return;
                }

                // We only care about methods with parameters.
                var method = (IMethodSymbol)startCodeBlockContext.OwningSymbol;
                if (method.Parameters.IsEmpty)
                {
                    return;
                }

                // Initialize local mutable state in the start action.
                var analyzer = new UnusedParametersAnalyzer(method);

                // Register an intermediate non-end action that accesses and modifies the state.
                startCodeBlockContext.RegisterSyntaxNodeAction(analyzer.AnalyzeSyntaxNode, SyntaxKind.IdentifierName);

                // Register an end action to report diagnostics based on the final state.
                startCodeBlockContext.RegisterCodeBlockEndAction(analyzer.CodeBlockEndAction);
            });
        }
開發者ID:Rickinio,項目名稱:roslyn,代碼行數:27,代碼來源:CodeBlockStartedAnalyzer.cs

示例14: Initialize

 public override void Initialize(AnalysisContext context)
 {
     context.RegisterSyntaxNodeAction(
     	AnalyzeInvocation, 
         new SyntaxKind[] { SyntaxKind.InvocationExpression }
     );
 }
開發者ID:alecor191,項目名稱:RefactoringEssentials,代碼行數:7,代碼來源:FormatStringProblemAnalyzer.cs

示例15: Initialize

        /// <inheritdoc/>
        public override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();

            context.RegisterSyntaxNodeAction(DocumentationTriviaAction, SyntaxKind.SingleLineDocumentationCommentTrivia);
        }
開發者ID:Romanx,項目名稱:StyleCopAnalyzers,代碼行數:8,代碼來源:SA1612ElementParameterDocumentationMustMatchElementParameters.cs


注:本文中的AnalysisContext類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。