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
ContainerDefinitionsExtractor.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ContainerDefinitionsExtractor.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 the containers of a type</summary>
7//-----------------------------------------------------------------------
8using Microsoft.CodeAnalysis;
9using Microsoft.CodeAnalysis.CSharp.Syntax;
10using System;
11using System.Collections.Generic;
12using System.Text;
13
15{
16 internal static class ContainerDefinitionsExtractor
17 {
18
25 public static IReadOnlyList<ExtractedContainerDefinition> GetContainerDefinitions(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
26 {
27 NamespaceDeclarationSyntax namespaceDeclaration;
28 TypeDeclarationSyntax containingTypeDeclaration;
29 List<ExtractedContainerDefinition> containers = new List<ExtractedContainerDefinition>();
30
31 // Iterate through the containing types should the target type be nested inside other types
32 containingTypeDeclaration = targetTypeDeclaration;
33 while (containingTypeDeclaration.Parent is TypeDeclarationSyntax)
34 {
35 containingTypeDeclaration = (TypeDeclarationSyntax)containingTypeDeclaration.Parent;
36 containers.Add(GetContainerDefinition(extractionContext, containingTypeDeclaration));
37 }
38
39 namespaceDeclaration = containingTypeDeclaration.Parent as NamespaceDeclarationSyntax;
40 if (namespaceDeclaration is not null)
41 {
42 containers.Add(GetContainerDefinition(extractionContext, namespaceDeclaration));
43 }
44
45 containers.Reverse();
46
47 return containers;
48 }
49
50 private static ExtractedContainerDefinition GetContainerDefinition(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax typeDeclarationSyntax)
51 {
52 StringBuilder containerDefinitionBuilder = new StringBuilder();
53 ExtractedContainerDefinition containerDefinition;
54
55 foreach (SyntaxToken modifier in typeDeclarationSyntax.Modifiers)
56 {
57 containerDefinitionBuilder.Append(modifier.ToString());
58 containerDefinitionBuilder.Append(" ");
59 }
60
61 containerDefinitionBuilder.Append(typeDeclarationSyntax.Keyword.ToString());
62 containerDefinitionBuilder.Append(" ");
63 containerDefinitionBuilder.Append(typeDeclarationSyntax.Identifier.ToString());
64
65 containerDefinition = new ExtractedContainerDefinition()
66 {
67 Name = typeDeclarationSyntax.Identifier.ToString(),
68 FullDefinition = containerDefinitionBuilder.ToString()
69
70 };
71
72 return containerDefinition;
73 }
74
75 private static ExtractedContainerDefinition GetContainerDefinition(DefinitionExtractionContext extractionContext, NamespaceDeclarationSyntax namespaceDeclarationSyntax)
76 {
77 StringBuilder containerDefinitionBuilder = new StringBuilder();
78 ExtractedContainerDefinition containerDefinition;
79
80 foreach (SyntaxToken modifier in namespaceDeclarationSyntax.Modifiers)
81 {
82 containerDefinitionBuilder.Append(modifier.ToString());
83 containerDefinitionBuilder.Append(" ");
84 }
85
86 containerDefinitionBuilder.Append("namespace ");
87 containerDefinitionBuilder.Append(namespaceDeclarationSyntax.Name.ToString());
88
89 containerDefinition = new ExtractedContainerDefinition()
90 {
91 Name = namespaceDeclarationSyntax.Name.ToString(),
92 FullDefinition = containerDefinitionBuilder.ToString()
93
94 };
95
96 return containerDefinition;
97 }
98
99
100
101 }
102}