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.
EvaluateOperationAttributeUsageAnalyzer.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 operationUsageRule =
15 new DiagnosticDescriptor(
16 Constants.AnalyzerIdentifiers.IsOperationAttributeUsageCorrect, EvaluateOperationAttributeUsageAnalyzerConstants.Title,
17 EvaluateOperationAttributeUsageAnalyzerConstants.Message, Constants.Categories.Usage,
18 DiagnosticSeverity.Error, true,
19 helpLinkUri: HelpUrlBuilder.Build(
20 Constants.AnalyzerIdentifiers.IsOperationAttributeUsageCorrect, nameof(EvaluateOperationAttributeUsageAnalyzer)));
21
22 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(operationUsageRule);
23
24 public override void Initialize(AnalysisContext context)
25 {
26 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
27 context.EnableConcurrentExecution();
28 context.RegisterSyntaxNodeAction(AnalyzerAttributeDeclaration, SyntaxKind.Attribute);
29 }
30
31 private static void AnalyzerAttributeDeclaration(SyntaxNodeAnalysisContext context)
32 {
33 var attributeNode = (AttributeSyntax)context.Node;
34 var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeNode).Symbol.ContainingSymbol as ITypeSymbol;
35
36 if (attributeSymbol.IsDataPortalOperationAttribute())
37 {
38 var methodNode = attributeNode.FindParent<MethodDeclarationSyntax>();
39 var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
40 var typeSymbol = methodSymbol.ContainingType;
41
42 if (!typeSymbol.IsStereotype() || methodSymbol.IsStatic)
43 {
44 context.ReportDiagnostic(Diagnostic.Create(
45 operationUsageRule, attributeNode.GetLocation()));
46 }
47 }
48 }
49 }
50}
override ImmutableArray< DiagnosticDescriptor > SupportedDiagnostics