protected static void AnalyzeEnumICollectionProperty(SyntaxNodeAnalysisContext context, ExpressionStatementSyntax expression) { if (!AnalyzerUtils.LastNodeIsInvocationExpressionSyntax(expression, out var invocationExpressionSyntax)) { return; } var methodSymbol = context .SemanticModel .GetSymbolInfo(invocationExpressionSyntax, context.CancellationToken) .Symbol as IMethodSymbol; if (methodSymbol == null || !methodSymbol.IsGenericMethod || methodSymbol.Name != "Property") { return; } var returnTypeSymbol = methodSymbol.ReturnType; var iCollectionTypeSymbol = ((INamedTypeSymbol)returnTypeSymbol).TypeArguments.FirstOrDefault(); if (iCollectionTypeSymbol?.Name != nameof(ICollection)) { return; } var enumTypeSymbol = (iCollectionTypeSymbol as INamedTypeSymbol).TypeArguments.FirstOrDefault(); if (enumTypeSymbol?.TypeKind != TypeKind.Enum) { return; } var enumMembers = enumTypeSymbol.GetMembers() .Where(m => m.Kind == SymbolKind.Field) .ToList(); if (!enumMembers.Any()) { return; } var diagnostic = Diagnostic.Create(Rule, invocationExpressionSyntax.GetLocation(), methodSymbol.Name); context.ReportDiagnostic(diagnostic); }
protected static void AnalyzeQueryableSelectInvocation(SyntaxNodeAnalysisContext context, ExpressionStatementSyntax expression) { if (!AnalyzerUtils.LastNodeIsInvocationExpressionSyntax(expression, out var invocationExpressionSyntax)) { return; } var methodSymbol = context .SemanticModel .GetSymbolInfo(invocationExpressionSyntax, context.CancellationToken) .Symbol as IMethodSymbol; if (methodSymbol == null || !methodSymbol.IsGenericMethod || methodSymbol.Name == "Select") { return; } var returnTypeSymbol = methodSymbol.ReturnType; var genericTypeSymbol = ((INamedTypeSymbol)returnTypeSymbol).TypeArguments.First(); bool genericTypeHasProperties = genericTypeSymbol .GetMembers().Any(m => m.Kind == SymbolKind.Property); if (!genericTypeHasProperties) { return; } var compilation = context.Compilation; var iQueryableType = compilation.GetTypeByMetadataName("System.Linq.IQueryable`1"); var areEqual = SymbolEqualityComparer.Default.Equals(iQueryableType, returnTypeSymbol.OriginalDefinition); if (!areEqual) { return; } var diagnostic = Diagnostic.Create(Rule, invocationExpressionSyntax.GetLocation(), methodSymbol.Name); context.ReportDiagnostic(diagnostic); }