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
FieldDefinitionsExtractor.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="FieldDefinitionsExtractor.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Extract the definitions of all fields for source generation</summary>
7//-----------------------------------------------------------------------
8using Microsoft.CodeAnalysis;
9using Microsoft.CodeAnalysis.CSharp.Syntax;
10using System;
11using System.Collections.Generic;
12using System.Linq;
13using System.Text;
14
16{
17
24 internal static class FieldDefinitionsExtractor
25 {
26
33 public static IReadOnlyList<ExtractedFieldDefinition> ExtractFieldDefinitions(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
34 {
35 List<ExtractedFieldDefinition> propertyDefinitions = new List<ExtractedFieldDefinition>();
36 ExtractedFieldDefinition fieldDefinition;
37 IReadOnlyList<FieldDeclarationSyntax> serializableFields;
38
39 serializableFields = GetSerializableFieldDeclarations(extractionContext, targetTypeDeclaration);
40 foreach (FieldDeclarationSyntax fieldDeclaration in serializableFields)
41 {
42 fieldDefinition = FieldDefinitionExtractor.ExtractFieldDefinition(extractionContext, fieldDeclaration);
43 propertyDefinitions.Add(fieldDefinition);
44 }
45
46 return propertyDefinitions;
47 }
48
49 #region Private Helper Methods
50
57 private static IReadOnlyList<FieldDeclarationSyntax> GetSerializableFieldDeclarations(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
58 {
59 List<FieldDeclarationSyntax> serializableFields;
60 List<FieldDeclarationSyntax> optedInSerializableFields;
61
62 // Get all fields that are not specifically opted out with the [AutoNonSerialized] attribute
63 serializableFields = GetPublicNonExcludedFields(extractionContext, targetTypeDeclaration);
64
65 // Add any pfields that are opted in with the use of the [AutoSerialized] attribute
66 optedInSerializableFields = GetNonPublicIncludedFields(extractionContext, targetTypeDeclaration);
67 serializableFields.AddRange(optedInSerializableFields);
68 // serializableFields = GetAllIncludedFields(extractionContext, targetTypeDeclaration);
69
70 return serializableFields;
71 }
72
79 private static List<FieldDeclarationSyntax> GetPublicNonExcludedFields(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
80 {
81 List<FieldDeclarationSyntax> serializableFields;
82
83 // Get all fields that are not specifically opted out with the [AutoNonSerialized] attribute
84 serializableFields = targetTypeDeclaration.Members.Where(
85 m => m is FieldDeclarationSyntax fieldDeclaration &&
86 HasOneOfScopes(extractionContext, fieldDeclaration, "public") &&
87 !extractionContext.IsFieldDecoratedWithAutoNonSerialized(fieldDeclaration))
88 .Cast<FieldDeclarationSyntax>()
89 .ToList();
90
91 return serializableFields;
92 }
93
100 private static List<FieldDeclarationSyntax> GetNonPublicIncludedFields(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
101 {
102 List<FieldDeclarationSyntax> serializableFields;
103
104 // Get any private or protected fields that are opted in with the use of the [AutoSerialized] attribute
105 serializableFields = targetTypeDeclaration.Members.Where(
106 m => m is FieldDeclarationSyntax fieldDeclaration &&
107 !HasOneOfScopes(extractionContext, fieldDeclaration, "public") &&
108 extractionContext.IsFieldDecoratedWithAutoSerialized(fieldDeclaration))
109 .Cast<FieldDeclarationSyntax>()
110 .ToList();
111
112 return serializableFields;
113 }
114
121 private static List<FieldDeclarationSyntax> GetAllIncludedFields(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
122 {
123 List<FieldDeclarationSyntax> serializableFields;
124
125 // Get any private or protected fields that are opted in with the use of the [AutoSerialized] attribute
126 serializableFields = targetTypeDeclaration.Members.Where(
127 m => m is FieldDeclarationSyntax fieldDeclaration &&
128 extractionContext.IsFieldDecoratedWithAutoSerialized(fieldDeclaration))
129 .Cast<FieldDeclarationSyntax>()
130 .ToList();
131
132 return serializableFields;
133 }
134
142 private static bool HasOneOfScopes(DefinitionExtractionContext context, FieldDeclarationSyntax fieldDeclaration, params string[] scopes)
143 {
144 foreach (string scope in scopes)
145 {
146 if (fieldDeclaration.Modifiers.Any(m => m.ValueText.Equals(scope, StringComparison.InvariantCultureIgnoreCase)))
147 {
148 return true;
149 }
150 }
151
152 return false;
153 }
154
155 #endregion
156
157 }
158}