-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEnclosingClassExtractor.cs
More file actions
29 lines (22 loc) · 1.01 KB
/
EnclosingClassExtractor.cs
File metadata and controls
29 lines (22 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace AutoLoggerMessageGenerator.Extractors;
internal static class EnclosingClassExtractor
{
public static (string Namespace, string ClassName) Extract(InvocationExpressionSyntax invocationExpression)
{
string className = string.Empty, ns = string.Empty;
SyntaxNode syntaxNode = invocationExpression;
while (syntaxNode.Parent is not null)
{
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax)
className = classDeclarationSyntax.Identifier.Text;
if (syntaxNode is NamespaceDeclarationSyntax namespaceDeclarationSyntax)
ns = namespaceDeclarationSyntax.Name.ToString();
if (syntaxNode is FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
ns = fileScopedNamespaceDeclarationSyntax.Name.ToString();
syntaxNode = syntaxNode.Parent;
}
return (ns, className);
}
}