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.
FindOperationsWithNonSerializableArgumentsAnalyzer.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;
7using System.Linq;
8
9namespace Csla.Analyzers
10{
11 [DiagnosticAnalyzer(LanguageNames.CSharp)]
13 : DiagnosticAnalyzer
14 {
15 private static readonly DiagnosticDescriptor shouldUseSerializableTypesRule =
16 new DiagnosticDescriptor(
17 Constants.AnalyzerIdentifiers.FindOperationsWithNonSerializableArguments, FindOperationsWithNonSerializableArgumentsConstants.Title,
18 FindOperationsWithNonSerializableArgumentsConstants.Message, Constants.Categories.Design,
19 DiagnosticSeverity.Warning, true,
20 helpLinkUri: HelpUrlBuilder.Build(
21 Constants.AnalyzerIdentifiers.FindOperationsWithNonSerializableArguments, nameof(FindOperationsWithNonSerializableArgumentsAnalyzer)));
22
23 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
24 ImmutableArray.Create(shouldUseSerializableTypesRule);
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 var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
37 var typeSymbol = methodSymbol.ContainingType;
38
39 if (typeSymbol.IsStereotype() && methodSymbol.IsRootDataPortalOperation())
40 {
41 foreach(var argument in methodSymbol.Parameters)
42 {
43 var argumentType = argument.Type;
44 if (!argumentType.IsMobileObject() && !argumentType.IsSpecialTypeSerializable() &&
45 !argumentType.IsSerializableByMobileFormatter(context.Compilation) &&
46 !argument.GetAttributes().Any(_ => _.AttributeClass.IsInjectable()) &&
47 argumentType is INamedTypeSymbol namedArgument && !namedArgument.IsSerializable)
48 {
49 context.ReportDiagnostic(Diagnostic.Create(
50 shouldUseSerializableTypesRule, argument.Locations[0]));
51 }
52 }
53 }
54 }
55 }
56}