diff --git a/ReactWithDotNet.SourceGenerator/FastSerializeJsonConverterGenerator.cs b/ReactWithDotNet.SourceGenerator/FastSerializeJsonConverterGenerator.cs index 5bb361a9..4799a18e 100644 --- a/ReactWithDotNet.SourceGenerator/FastSerializeJsonConverterGenerator.cs +++ b/ReactWithDotNet.SourceGenerator/FastSerializeJsonConverterGenerator.cs @@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; +using static ReactWithDotNet.RoslynHelper; namespace ReactWithDotNet; @@ -67,6 +68,8 @@ public void Execute(GeneratorExecutionContext context) lines.Add("}"); continue; } + + var found = FindClassByNameAndNamespace(context.Compilation, propertyTypeName, GetNamespace(classDeclaration)); lines.Add($"if ({propertyTypeName}HasValueChecker.HasValue(value.{propertyName}))"); lines.Add("{"); @@ -92,21 +95,7 @@ public void Initialize(GeneratorInitializationContext context) context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); } - // Helper to extract namespace - static string GetNamespace(ClassDeclarationSyntax classDeclaration) - { - var fileScopedNamespaceName = classDeclaration.FirstAncestorOrSelf()?.Name; - if (fileScopedNamespaceName != null) - { - return fileScopedNamespaceName.ToString(); - } - - var namespaceDeclaration = classDeclaration.Ancestors() - .OfType() - .FirstOrDefault(); - - return namespaceDeclaration?.Name.ToString() ?? "global"; - } + class SyntaxReceiver : ISyntaxReceiver { diff --git a/ReactWithDotNet.SourceGenerator/RosalynHelper.cs b/ReactWithDotNet.SourceGenerator/RosalynHelper.cs new file mode 100644 index 00000000..9274f9f7 --- /dev/null +++ b/ReactWithDotNet.SourceGenerator/RosalynHelper.cs @@ -0,0 +1,61 @@ +namespace ReactWithDotNet; + + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using System.Linq; + +static class RoslynHelper +{ + public static string GetNamespace(ClassDeclarationSyntax classDeclaration) + { + var fileScopedNamespaceName = classDeclaration.FirstAncestorOrSelf()?.Name; + if (fileScopedNamespaceName != null) + { + return fileScopedNamespaceName.ToString(); + } + + var namespaceDeclaration = classDeclaration.Ancestors() + .OfType() + .FirstOrDefault(); + + return namespaceDeclaration?.Name.ToString() ?? "global"; + } + /// + /// Finds a class declaration by its name and namespace in the given compilation. + /// + /// The compilation to search through. + /// The name of the class to find. + /// The namespace of the class to find. + /// The ClassDeclarationSyntax if found, null otherwise. + public static ClassDeclarationSyntax FindClassByNameAndNamespace(Compilation compilation, string className, string namespaceName) + { + foreach (var syntaxTree in compilation.SyntaxTrees) + { + var root = syntaxTree.GetRoot(); + + // Find all class declarations in the current syntax tree + var classDeclarations = root.DescendantNodes() + .OfType(); + + // Loop through each class declaration and check its name and namespace + foreach (var classDeclaration in classDeclarations) + { + // Check if the class name matches + if (classDeclaration.Identifier.Text == className) + { + + + if (GetNamespace(classDeclaration) == namespaceName) + { + return classDeclaration; // Class found + } + } + } + } + + return null; // Class not found + } +} + +