当前位置: 首页>>代码示例>>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;未经允许,请勿转载。