CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
FindRefAndOutParametersInOperationsAnalyzer.cs
Go to the documentation of this file.
2using Microsoft.CodeAnalysis;
3using Microsoft.CodeAnalysis.CSharp;
4using Microsoft.CodeAnalysis.CSharp.Syntax;
5using Microsoft.CodeAnalysis.Diagnostics;
6using System.Collections.Immutable;
7
8namespace Csla.Analyzers
9{
10 [DiagnosticAnalyzer(LanguageNames.CSharp)]
12 : DiagnosticAnalyzer
13 {
14 private static readonly DiagnosticDescriptor incorrectParameterRule =
15 new DiagnosticDescriptor(
16 Constants.AnalyzerIdentifiers.RefOrOutParameterInOperation,
17 FindRefAndOutParametersInOperationsAnalyzerConstants.Title,
18 FindRefAndOutParametersInOperationsAnalyzerConstants.Message,
19 Constants.Categories.Usage, DiagnosticSeverity.Error, true,
20 helpLinkUri: HelpUrlBuilder.Build(
21 Constants.AnalyzerIdentifiers.RefOrOutParameterInOperation, nameof(FindRefAndOutParametersInOperationsAnalyzer)));
22
23 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
24 ImmutableArray.Create(incorrectParameterRule);
25
26 public override void Initialize(AnalysisContext context)
27 {
28 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
29 context.EnableConcurrentExecution();
30 context.RegisterSyntaxNodeAction(AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration);
31 }
32
33 private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
34 {
35 var methodNode = (MethodDeclarationSyntax)context.Node;
36
37 if (!methodNode.ContainsDiagnostics)
38 {
39 var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
40 var typeSymbol = methodSymbol.ContainingType;
41
42 if (typeSymbol.IsBusinessBase() && methodSymbol.IsDataPortalOperation())
43 {
44 foreach(var parameterSymbol in methodSymbol.Parameters)
45 {
46 if(parameterSymbol.RefKind == RefKind.Out ||
47 parameterSymbol.RefKind == RefKind.Ref)
48 {
49 context.ReportDiagnostic(Diagnostic.Create(
50 incorrectParameterRule, parameterSymbol.Locations[0]));
51 }
52 }
53 }
54 }
55 }
56 }
57}