本文整理汇总了C#中AnalysisContext类的典型用法代码示例。如果您正苦于以下问题:C# AnalysisContext类的具体用法?C# AnalysisContext怎么用?C# AnalysisContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AnalysisContext类属于命名空间,在下文中一共展示了AnalysisContext类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: Initialize
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterSyntaxNodeAction(
(nodeContext) =>
{
Diagnostic diagnostic;
if (TryGetDiagnostic(nodeContext, out diagnostic))
{
nodeContext.ReportDiagnostic(diagnostic);
}
},
new SyntaxKind[] { SyntaxKind.LocalDeclarationStatement }
);
context.RegisterSyntaxNodeAction(
(nodeContext) =>
{
Diagnostic diagnostic;
if (TryGetDiagnosticFromForeach(nodeContext, out diagnostic))
{
nodeContext.ReportDiagnostic(diagnostic);
}
},
new SyntaxKind[] { SyntaxKind.ForEachStatement }
);
}
开发者ID:petterek,项目名称:RefactoringEssentials,代码行数:26,代码来源:LocalVariableHidesMemberAnalyzer.cs
示例17: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var assignment = (AssignmentExpressionSyntax)c.Node;
if (!IsDelegateSubtraction(assignment, c.SemanticModel) ||
ExpressionIsSimple(assignment.Right))
{
return;
}
c.ReportDiagnostic(Diagnostic.Create(Rule, assignment.GetLocation()));
},
SyntaxKind.SubtractAssignmentExpression);
context.RegisterSyntaxNodeAction(
c =>
{
var binary = (BinaryExpressionSyntax)c.Node;
if (!IsDelegateSubtraction(binary, c.SemanticModel) ||
!IsTopLevelSubtraction(binary))
{
return;
}
if (!BinaryIsValidSubstraction(binary))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, binary.GetLocation()));
}
},
SyntaxKind.SubtractExpression);
}
开发者ID:ozgurkayaist,项目名称:sonarlint-vs,代码行数:33,代码来源:DelegateSubtraction.cs
示例18: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterCodeBlockStartActionInNonGenerated<SyntaxKind>(
cbc =>
{
var methodDeclaration = cbc.CodeBlock as MethodDeclarationSyntax;
if (methodDeclaration == null ||
methodDeclaration.Identifier.Text != "ToString")
{
return;
}
cbc.RegisterSyntaxNodeAction(c =>
{
var returnStatement = (ReturnStatementSyntax)c.Node;
var nullExpression = returnStatement.Expression as LiteralExpressionSyntax;
if (nullExpression != null && nullExpression.IsKind(SyntaxKind.NullLiteralExpression))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, returnStatement.GetLocation()));
}
}, SyntaxKind.ReturnStatement);
});
}
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:26,代码来源:ToStringNoNull.cs
示例19: Initialize
public override void Initialize(AnalysisContext analysisContext)
{
analysisContext.EnableConcurrentExecution();
analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
analysisContext.RegisterSymbolAction(
symbolAnalysisContext =>
{
ISymbol symbol = symbolAnalysisContext.Symbol;
if (!symbol.ContainingType.IsGenericType ||
symbol.DeclaredAccessibility != Accessibility.Public ||
!symbol.IsStatic)
{
return;
}
var methodSymbol = symbol as IMethodSymbol;
if (methodSymbol != null &&
(methodSymbol.IsAccessorMethod() ||
(methodSymbol.MethodKind == MethodKind.UserDefinedOperator &&
(methodSymbol.Name == WellKnownMemberNames.EqualityOperatorName ||
methodSymbol.Name == WellKnownMemberNames.InequalityOperatorName))))
{
return;
}
symbolAnalysisContext.ReportDiagnostic(symbol.CreateDiagnostic(Rule, symbol.Name));
}, SymbolKind.Method, SymbolKind.Property);
}
开发者ID:duracellko,项目名称:roslyn-analyzers,代码行数:29,代码来源:DoNotDeclareStaticMembersOnGenericTypes.cs
示例20: Initialize
public override void Initialize(AnalysisContext analysisContext)
{
analysisContext.RegisterCompilationStartAction(compilationContext =>
{
INamedTypeSymbol conditionalAttributeSymbol = WellKnownTypes.ConditionalAttribute(compilationContext.Compilation);
compilationContext.RegisterOperationBlockAction(context =>
{
var method = context.OwningSymbol as IMethodSymbol;
if (method == null)
{
return;
}
if (!method.IsFinalizer())
{
return;
}
if (IsEmptyFinalizer(context.OperationBlocks, conditionalAttributeSymbol))
{
context.ReportDiagnostic(context.OwningSymbol.CreateDiagnostic(Rule));
}
});
});
}
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:26,代码来源:RemoveEmptyFinalizers.cs
注:本文中的AnalysisContext类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论