10using Microsoft.CodeAnalysis.CSharp.Syntax;
12using System.Collections.Generic;
22 internal class DefinitionExtractionContext
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;
31 public DefinitionExtractionContext(GeneratorSyntaxContext 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);
40 public GeneratorSyntaxContext Context => _context;
47 public string GetTypeNamespace(TypeDeclarationSyntax typeDeclarationSyntax)
49 INamedTypeSymbol typeSymbol;
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();
61 public string GetTypeNamespace(TypeSyntax typeSyntax)
63 INamedTypeSymbol typeSymbol;
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();
75 public bool IsTypeAutoSerializable(TypeDeclarationSyntax typeDeclarationSyntax)
77 INamedTypeSymbol typeSymbol;
79 typeSymbol = _context.SemanticModel.GetDeclaredSymbol(typeDeclarationSyntax) as INamedTypeSymbol;
80 return IsTypeDecoratedBy(typeSymbol, _autoSerializableAttributeSymbol);
88 public bool IsTypeAutoSerializable(TypeSyntax typeSyntax)
90 INamedTypeSymbol typeSymbol;
92 typeSymbol = _context.SemanticModel.GetSymbolInfo(typeSyntax).Symbol as INamedTypeSymbol;
93 if (typeSymbol is
null)
return false;
94 return IsTypeDecoratedBy(typeSymbol, _autoSerializableAttributeSymbol);
103 public bool DoesTypeImplementIMobileObject(TypeSyntax typeSyntax)
105 INamedTypeSymbol typeSymbol;
107 typeSymbol = _context.SemanticModel.GetSymbolInfo(typeSyntax).Symbol as INamedTypeSymbol;
108 if (typeSymbol is
null)
return false;
110 foreach (ITypeSymbol interfaceSymbol
in typeSymbol.AllInterfaces)
112 if (IsMatchingTypeSymbol(interfaceSymbol as INamedTypeSymbol, _mobileObjectInterfaceSymbol))
126 public bool IsPropertyDecoratedWithAutoSerialized(PropertyDeclarationSyntax propertyDeclaration)
128 return IsPropertyDecoratedWith(propertyDeclaration, _autoSerializedAttributeSymbol);
136 public bool IsPropertyDecoratedWithAutoNonSerialized(PropertyDeclarationSyntax propertyDeclaration)
138 return IsPropertyDecoratedWith(propertyDeclaration, _autoNonSerializedAttributeSymbol);
146 public bool IsFieldDecoratedWithAutoSerialized(FieldDeclarationSyntax fieldDeclaration)
148 return IsFieldDecoratedWith(fieldDeclaration, _autoSerializedAttributeSymbol);
156 public bool IsFieldDecoratedWithAutoNonSerialized(FieldDeclarationSyntax fieldDeclaration)
158 return IsFieldDecoratedWith(fieldDeclaration, _autoNonSerializedAttributeSymbol);
161 #region Private Helper Methods
169 private bool IsTypeDecoratedBy(INamedTypeSymbol typeSymbol, INamedTypeSymbol desiredAttributeSymbol)
171 return typeSymbol.GetAttributes().Any(
172 attr => IsMatchingTypeSymbol(attr.AttributeClass, desiredAttributeSymbol));
181 private bool IsPropertyDecoratedWith(PropertyDeclarationSyntax propertyDeclaration, INamedTypeSymbol desiredAttributeSymbol)
183 INamedTypeSymbol appliedAttributeSymbol;
185 foreach (AttributeSyntax attributeSyntax
in propertyDeclaration.AttributeLists.SelectMany(al => al.Attributes))
187 appliedAttributeSymbol = _context.SemanticModel.GetTypeInfo(attributeSyntax).Type as INamedTypeSymbol;
188 if (IsMatchingTypeSymbol(appliedAttributeSymbol, desiredAttributeSymbol))
202 private bool IsFieldDecoratedWith(FieldDeclarationSyntax fieldDeclaration, INamedTypeSymbol desiredAttributeSymbol)
204 INamedTypeSymbol appliedAttributeSymbol;
206 foreach (AttributeSyntax attributeSyntax
in fieldDeclaration.AttributeLists.SelectMany(al => al.Attributes))
208 appliedAttributeSymbol = _context.SemanticModel.GetTypeInfo(attributeSyntax).Type as INamedTypeSymbol;
209 if (IsMatchingTypeSymbol(appliedAttributeSymbol, desiredAttributeSymbol))
223 private bool IsMatchingTypeSymbol(INamedTypeSymbol appliedAttributeSymbol, INamedTypeSymbol desiredAttributeSymbol)
225 return SymbolEqualityComparer.Default.Equals(appliedAttributeSymbol, desiredAttributeSymbol);
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...