From 3fb281031c43760862f08d027a766acd8d4d0a28 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Wed, 14 Jan 2026 13:12:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(logging):=20=E6=B7=BB=E5=8A=A0=E6=B3=9B?= =?UTF-8?q?=E5=9E=8B=E7=B1=BB=E7=9A=84=E6=97=A5=E5=BF=97=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了对泛型类的支持,包括类型参数和约束的处理 - 实现了泛型约束的代码生成功能 - 增加了对引用类型、值类型、构造函数约束的支持 - 重构了类声明的生成逻辑以支持泛型参数 - 添加了必要的命名空间引用和集合操作支持 --- .../logging/LoggerGenerator.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/GFramework.SourceGenerators/logging/LoggerGenerator.cs b/GFramework.SourceGenerators/logging/LoggerGenerator.cs index 8ace031..3aaa4a8 100644 --- a/GFramework.SourceGenerators/logging/LoggerGenerator.cs +++ b/GFramework.SourceGenerators/logging/LoggerGenerator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; using GFramework.SourceGenerators.Abstractions.logging; @@ -77,6 +78,26 @@ public sealed class LoggerGenerator : TypeAttributeClassGeneratorBase var isStatic = isStaticObj is not bool b || b; // 默认 true var staticKeyword = isStatic ? "static " : ""; + // 泛型参数和约束 + var typeParams = symbol.TypeParameters.Length > 0 + ? "<" + string.Join(", ", symbol.TypeParameters.Select(tp => tp.Name)) + ">" + : ""; + + var constraints = symbol.TypeParameters + .Where(tp => tp.ConstraintTypes.Length > 0 || tp.HasReferenceTypeConstraint || tp.HasValueTypeConstraint || + tp.HasConstructorConstraint) + .Select(tp => + { + var parts = new List(); + + if (tp.HasReferenceTypeConstraint) parts.Add("class"); + if (tp.HasValueTypeConstraint) parts.Add("struct"); + parts.AddRange(tp.ConstraintTypes.Select(t => t.ToDisplayString())); + if (tp.HasConstructorConstraint) parts.Add("new()"); + + return $"where {tp.Name} : {string.Join(", ", parts)}"; + }); + var sb = new StringBuilder(); sb.AppendLine("// "); sb.AppendLine($"using {PathContests.CoreAbstractionsNamespace}.logging;"); @@ -88,7 +109,12 @@ public sealed class LoggerGenerator : TypeAttributeClassGeneratorBase sb.AppendLine(); } - sb.AppendLine($"partial class {className}"); + sb.AppendLine($"partial class {className}{typeParams}"); + foreach (var c in constraints) + { + sb.AppendLine($" {c}"); + } + sb.AppendLine("{"); sb.AppendLine(" /// Auto-generated logger"); sb.AppendLine( @@ -98,6 +124,7 @@ public sealed class LoggerGenerator : TypeAttributeClassGeneratorBase return sb.ToString().TrimEnd(); } + /// /// 可以自定义生成文件名 ///