File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -36662,25 +36662,39 @@ public class ToStringGenerator : IIncrementalGenerator
3666236662
3666336663 private static string GenerateToString(INamedTypeSymbol symbol)
3666436664 {
36665- var ns = symbol.ContainingNamespace.ToDisplayString();
36665+
36666+ var ns = symbol.ContainingNamespace.IsGlobalNamespace
36667+ ? null
36668+ : symbol.ContainingNamespace.ToDisplayString();
36669+
3666636670 var props = symbol.GetMembers()
3666736671 .OfType<IPropertySymbol>()
3666836672 .Where(p => p.DeclaredAccessibility == Accessibility.Public);
3666936673
36674+
36675+ var accessibility = symbol.DeclaredAccessibility.ToString().ToLower();
36676+
36677+ var propString = string.Join(", ", props.Select(p => $"{p.Name} = {{{p.Name}}}"));
36678+
3667036679 var sb = new StringBuilder();
3667136680 sb.AppendLine("// <auto-generated/>");
36672- sb.AppendLine($"namespace {ns};");
36673- sb.AppendLine();
36674- sb.AppendLine($"partial class {symbol.Name}");
36675- sb.AppendLine("{");
36676- sb.Append(" public override string ToString() => $\"");
36677- sb.Append(symbol.Name);
36678- sb.Append(" {{ ");
36679- sb.Append(string.Join(", ", props.Select(p => $"{p.Name} = {{{p.Name}}}")));
36680- sb.AppendLine(" }}\";");
36681- sb.AppendLine("}");
36681+
36682+ if (ns != null)
36683+ {
36684+ sb.AppendLine($"namespace {ns};");
36685+ sb.AppendLine();
36686+ }
36687+
36688+ sb.Append($$"""
36689+ {{accessibility}} partial class {{symbol.Name}}
36690+ {
36691+ public override string ToString() => $"{{symbol.Name}} {{ {{propString}} }}";
36692+ }
36693+ """);
36694+
3668236695 return sb.ToString();
3668336696 }
36697+
3668436698}
3668536699
3668636700// ” 2. Attribute trigger (add to generator project) ”—————————————————
You can’t perform that action at this time.
0 commit comments