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.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DefinitionExtractionContext.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DefinitionExtractionContext.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Helper for definition extraction, used to optimise symbo, recognition</summary>
7//-----------------------------------------------------------------------
9using Microsoft.CodeAnalysis;
10using Microsoft.CodeAnalysis.CSharp.Syntax;
11using System;
12using System.Collections.Generic;
13using System.Linq;
14using System.Text;
15
17{
18
22 internal class DefinitionExtractionContext
23 {
24
25 private readonly GeneratorSyntaxContext _context;
26 private readonly INamedTypeSymbol _autoSerializableAttributeSymbol;
27 private readonly INamedTypeSymbol _autoSerializedAttributeSymbol;
28 private readonly INamedTypeSymbol _autoNonSerializedAttributeSymbol;
29 private readonly INamedTypeSymbol _mobileObjectInterfaceSymbol;
30
31 public DefinitionExtractionContext(GeneratorSyntaxContext context)
32 {
33 _context = context;
34 _autoSerializableAttributeSymbol = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoSerializableAttribute).FullName);
35 _autoSerializedAttributeSymbol = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoSerializedAttribute).FullName);
36 _autoNonSerializedAttributeSymbol = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoNonSerializedAttribute).FullName);
37 _mobileObjectInterfaceSymbol = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(Csla.Serialization.Mobile.IMobileObject).FullName);
38 }
39
40 public GeneratorSyntaxContext Context => _context;
41
47 public string GetTypeNamespace(TypeDeclarationSyntax typeDeclarationSyntax)
48 {
49 INamedTypeSymbol typeSymbol;
50
51 typeSymbol = _context.SemanticModel.GetDeclaredSymbol(typeDeclarationSyntax) as INamedTypeSymbol;
52 if (typeSymbol is null || typeSymbol.ContainingNamespace is null) return string.Empty;
53 return typeSymbol.ContainingNamespace.ToString();
54 }
55
61 public string GetTypeNamespace(TypeSyntax typeSyntax)
62 {
63 INamedTypeSymbol typeSymbol;
64
65 typeSymbol = _context.SemanticModel.GetSymbolInfo(typeSyntax).Symbol as INamedTypeSymbol;
66 if (typeSymbol is null || typeSymbol.ContainingNamespace is null) return string.Empty;
67 return typeSymbol.ContainingNamespace.ToString();
68 }
69
75 public bool IsTypeAutoSerializable(TypeDeclarationSyntax typeDeclarationSyntax)
76 {
77 INamedTypeSymbol typeSymbol;
78
79 typeSymbol = _context.SemanticModel.GetDeclaredSymbol(typeDeclarationSyntax) as INamedTypeSymbol;
80 return IsTypeDecoratedBy(typeSymbol, _autoSerializableAttributeSymbol);
81 }
82
88 public bool IsTypeAutoSerializable(TypeSyntax typeSyntax)
89 {
90 INamedTypeSymbol typeSymbol;
91
92 typeSymbol = _context.SemanticModel.GetSymbolInfo(typeSyntax).Symbol as INamedTypeSymbol;
93 if (typeSymbol is null) return false;
94 return IsTypeDecoratedBy(typeSymbol, _autoSerializableAttributeSymbol);
95 }
96
103 public bool DoesTypeImplementIMobileObject(TypeSyntax typeSyntax)
104 {
105 INamedTypeSymbol typeSymbol;
106
107 typeSymbol = _context.SemanticModel.GetSymbolInfo(typeSyntax).Symbol as INamedTypeSymbol;
108 if (typeSymbol is null) return false;
109
110 foreach (ITypeSymbol interfaceSymbol in typeSymbol.AllInterfaces)
111 {
112 if (IsMatchingTypeSymbol(interfaceSymbol as INamedTypeSymbol, _mobileObjectInterfaceSymbol))
113 {
114 return true;
115 }
116 }
117
118 return false;
119 }
120
126 public bool IsPropertyDecoratedWithAutoSerialized(PropertyDeclarationSyntax propertyDeclaration)
127 {
128 return IsPropertyDecoratedWith(propertyDeclaration, _autoSerializedAttributeSymbol);
129 }
130
136 public bool IsPropertyDecoratedWithAutoNonSerialized(PropertyDeclarationSyntax propertyDeclaration)
137 {
138 return IsPropertyDecoratedWith(propertyDeclaration, _autoNonSerializedAttributeSymbol);
139 }
140
146 public bool IsFieldDecoratedWithAutoSerialized(FieldDeclarationSyntax fieldDeclaration)
147 {
148 return IsFieldDecoratedWith(fieldDeclaration, _autoSerializedAttributeSymbol);
149 }
150
156 public bool IsFieldDecoratedWithAutoNonSerialized(FieldDeclarationSyntax fieldDeclaration)
157 {
158 return IsFieldDecoratedWith(fieldDeclaration, _autoNonSerializedAttributeSymbol);
159 }
160
161 #region Private Helper Methods
162
169 private bool IsTypeDecoratedBy(INamedTypeSymbol typeSymbol, INamedTypeSymbol desiredAttributeSymbol)
170 {
171 return typeSymbol.GetAttributes().Any(
172 attr => IsMatchingTypeSymbol(attr.AttributeClass, desiredAttributeSymbol));
173 }
174
181 private bool IsPropertyDecoratedWith(PropertyDeclarationSyntax propertyDeclaration, INamedTypeSymbol desiredAttributeSymbol)
182 {
183 INamedTypeSymbol appliedAttributeSymbol;
184
185 foreach (AttributeSyntax attributeSyntax in propertyDeclaration.AttributeLists.SelectMany(al => al.Attributes))
186 {
187 appliedAttributeSymbol = _context.SemanticModel.GetTypeInfo(attributeSyntax).Type as INamedTypeSymbol;
188 if (IsMatchingTypeSymbol(appliedAttributeSymbol, desiredAttributeSymbol))
189 {
190 return true;
191 }
192 }
193 return false;
194 }
195
202 private bool IsFieldDecoratedWith(FieldDeclarationSyntax fieldDeclaration, INamedTypeSymbol desiredAttributeSymbol)
203 {
204 INamedTypeSymbol appliedAttributeSymbol;
205
206 foreach (AttributeSyntax attributeSyntax in fieldDeclaration.AttributeLists.SelectMany(al => al.Attributes))
207 {
208 appliedAttributeSymbol = _context.SemanticModel.GetTypeInfo(attributeSyntax).Type as INamedTypeSymbol;
209 if (IsMatchingTypeSymbol(appliedAttributeSymbol, desiredAttributeSymbol))
210 {
211 return true;
212 }
213 }
214 return false;
215 }
216
223 private bool IsMatchingTypeSymbol(INamedTypeSymbol appliedAttributeSymbol, INamedTypeSymbol desiredAttributeSymbol)
224 {
225 return SymbolEqualityComparer.Default.Equals(appliedAttributeSymbol, desiredAttributeSymbol);
226 }
227
228 #endregion
229
230 }
231}
Indicate that a public field or property should be excluded from auto serialization
Indicate that a type should be auto serialized
Indicate that a non-public field or property should be included in auto serialization
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...