Skip to content

Commit c005a93

Browse files
Fixed namespace generation issue #4455 (#4458)
Co-authored-by: Rockford Lhotka <rocky@lhotka.net>
1 parent ee40e57 commit c005a93

2 files changed

Lines changed: 68 additions & 24 deletions

File tree

Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/AutoImplement/Discovery/TypeDefinitionExtractor.cs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,46 @@ private static string GetBaseClassTypeName(DefinitionExtractionContext extractio
138138
/// <returns>The namespace of the type for which generation is being performed</returns>
139139
private static string GetNamespace(TypeDeclarationSyntax targetTypeDeclaration)
140140
{
141-
string namespaceName = string.Empty;
142-
NamespaceDeclarationSyntax namespaceDeclaration;
143-
TypeDeclarationSyntax containingTypeDeclaration;
144-
145-
// Iterate through the containing types should the target type be nested inside other types
146-
containingTypeDeclaration = targetTypeDeclaration;
147-
while (containingTypeDeclaration.Parent is TypeDeclarationSyntax syntax)
141+
// If we don't have a namespace at all we'll return an empty string
142+
// This accounts for the "default namespace" case
143+
string nameSpace = string.Empty;
144+
145+
// Get the containing syntax node for the type declaration
146+
// (could be a nested type, for example)
147+
SyntaxNode potentialNamespaceParent = targetTypeDeclaration.Parent;
148+
149+
// Keep moving "out" of nested classes etc until we get to a namespace
150+
// or until we run out of parents
151+
while (potentialNamespaceParent != null &&
152+
potentialNamespaceParent is not NamespaceDeclarationSyntax
153+
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax)
148154
{
149-
containingTypeDeclaration = syntax;
155+
potentialNamespaceParent = potentialNamespaceParent.Parent;
150156
}
151157

152-
namespaceDeclaration = containingTypeDeclaration.Parent as NamespaceDeclarationSyntax;
153-
if (namespaceDeclaration is not null)
158+
// Build up the final namespace by looping until we no longer have a namespace declaration
159+
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
154160
{
155-
namespaceName = namespaceDeclaration.Name.ToString();
161+
// We have a namespace. Use that as the type
162+
nameSpace = namespaceParent.Name.ToString();
163+
164+
// Keep moving "out" of the namespace declarations until we
165+
// run out of nested namespace declarations
166+
while (true)
167+
{
168+
if (namespaceParent.Parent is not NamespaceDeclarationSyntax parent)
169+
{
170+
break;
171+
}
172+
173+
// Add the outer namespace as a prefix to the final namespace
174+
nameSpace = $"{namespaceParent.Name}.{nameSpace}";
175+
namespaceParent = parent;
176+
}
156177
}
157178

158-
return namespaceName;
179+
// return the final namespace
180+
return nameSpace;
159181
}
160182

161183
/// <summary>

Source/Csla.Generators/cs/AutoSerialization/Csla.Generator.AutoSerialization.CSharp/AutoSerializable/Discovery/TypeDefinitionExtractor.cs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,46 @@ public static ExtractedTypeDefinition ExtractTypeDefinition(DefinitionExtraction
6868
/// <returns>The namespace of the type for which generation is being performed</returns>
6969
private static string GetNamespace(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
7070
{
71-
string namespaceName = string.Empty;
72-
NamespaceDeclarationSyntax namespaceDeclaration;
73-
TypeDeclarationSyntax containingTypeDeclaration;
74-
75-
// Iterate through the containing types should the target type be nested inside other types
76-
containingTypeDeclaration = targetTypeDeclaration;
77-
while (containingTypeDeclaration.Parent is TypeDeclarationSyntax syntax)
71+
// If we don't have a namespace at all we'll return an empty string
72+
// This accounts for the "default namespace" case
73+
string nameSpace = string.Empty;
74+
75+
// Get the containing syntax node for the type declaration
76+
// (could be a nested type, for example)
77+
SyntaxNode potentialNamespaceParent = targetTypeDeclaration.Parent;
78+
79+
// Keep moving "out" of nested classes etc until we get to a namespace
80+
// or until we run out of parents
81+
while (potentialNamespaceParent != null &&
82+
potentialNamespaceParent is not NamespaceDeclarationSyntax
83+
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax)
7884
{
79-
containingTypeDeclaration = syntax;
85+
potentialNamespaceParent = potentialNamespaceParent.Parent;
8086
}
8187

82-
namespaceDeclaration = containingTypeDeclaration.Parent as NamespaceDeclarationSyntax;
83-
if (namespaceDeclaration is not null)
88+
// Build up the final namespace by looping until we no longer have a namespace declaration
89+
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
8490
{
85-
namespaceName = namespaceDeclaration.Name.ToString();
91+
// We have a namespace. Use that as the type
92+
nameSpace = namespaceParent.Name.ToString();
93+
94+
// Keep moving "out" of the namespace declarations until we
95+
// run out of nested namespace declarations
96+
while (true)
97+
{
98+
if (namespaceParent.Parent is not NamespaceDeclarationSyntax parent)
99+
{
100+
break;
101+
}
102+
103+
// Add the outer namespace as a prefix to the final namespace
104+
nameSpace = $"{namespaceParent.Name}.{nameSpace}";
105+
namespaceParent = parent;
106+
}
86107
}
87108

88-
return namespaceName;
109+
// return the final namespace
110+
return nameSpace;
89111
}
90112

91113
/// <summary>

0 commit comments

Comments
 (0)