10using Microsoft.CodeAnalysis.CSharp.Syntax;
12using System.Collections.Generic;
25 internal static class PropertyDefinitionsExtractor
34 public static IReadOnlyList<ExtractedPropertyDefinition> ExtractPropertyDefinitions(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
36 List<ExtractedPropertyDefinition> propertyDefinitions =
new List<ExtractedPropertyDefinition>();
37 ExtractedPropertyDefinition propertyDefinition;
38 IReadOnlyList<PropertyDeclarationSyntax> serializableProperties;
40 serializableProperties = GetSerializablePropertyDeclarations(extractionContext, targetTypeDeclaration);
41 foreach (PropertyDeclarationSyntax propertyDeclaration
in serializableProperties)
43 propertyDefinition = PropertyDefinitionExtractor.ExtractPropertyDefinition(extractionContext, propertyDeclaration);
44 propertyDefinitions.Add(propertyDefinition);
47 return propertyDefinitions;
50 #region Private Helper Methods
58 private static IReadOnlyList<PropertyDeclarationSyntax> GetSerializablePropertyDeclarations(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
60 List<PropertyDeclarationSyntax> serializableProperties;
61 List<PropertyDeclarationSyntax> optedInSerializableProperties;
64 serializableProperties = GetPublicNonExcludedProperties(extractionContext, targetTypeDeclaration);
67 optedInSerializableProperties = GetNonPublicIncludedProperties(extractionContext, targetTypeDeclaration);
68 serializableProperties.AddRange(optedInSerializableProperties);
70 return serializableProperties;
79 private static List<PropertyDeclarationSyntax> GetPublicNonExcludedProperties(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
81 List<PropertyDeclarationSyntax> serializableProperties;
84 serializableProperties = targetTypeDeclaration.Members.Where(
85 m => m is PropertyDeclarationSyntax propertyDeclaration &&
86 HasOneOfScopes(extractionContext, propertyDeclaration,
"public") &&
87 HasGetterAndSetter(extractionContext, propertyDeclaration) &&
88 !extractionContext.IsPropertyDecoratedWithAutoNonSerialized(propertyDeclaration))
89 .Cast<PropertyDeclarationSyntax>()
92 return serializableProperties;
101 private static List<PropertyDeclarationSyntax> GetNonPublicIncludedProperties(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
103 List<PropertyDeclarationSyntax> serializableProperties;
106 serializableProperties = targetTypeDeclaration.Members.Where(
107 m => m is PropertyDeclarationSyntax propertyDeclaration &&
108 !HasOneOfScopes(extractionContext, propertyDeclaration,
"public") &&
109 HasGetterAndSetter(extractionContext, propertyDeclaration) &&
110 extractionContext.IsPropertyDecoratedWithAutoSerialized(propertyDeclaration))
111 .Cast<PropertyDeclarationSyntax>()
114 return serializableProperties;
124 private static bool HasOneOfScopes(DefinitionExtractionContext context, PropertyDeclarationSyntax propertyDeclaration, params
string[] scopes)
126 foreach (
string scope
in scopes)
128 if (propertyDeclaration.Modifiers.Any(m => m.ValueText.Equals(scope, StringComparison.InvariantCultureIgnoreCase)))
143 private static bool HasGetterAndSetter(DefinitionExtractionContext context, PropertyDeclarationSyntax propertyDeclaration)
145 bool hasGetter =
false;
146 bool hasSetter =
false;
148 if (propertyDeclaration.AccessorList is
null)
return false;
150 foreach (AccessorDeclarationSyntax accessorDeclaration
in propertyDeclaration.AccessorList.Accessors)
152 if (accessorDeclaration.Kind() ==
Microsoft.CodeAnalysis.CSharp.SyntaxKind.GetAccessorDeclaration)
156 if (accessorDeclaration.Kind() ==
Microsoft.CodeAnalysis.CSharp.SyntaxKind.SetAccessorDeclaration)
162 return hasGetter && hasSetter;