mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(logging): 添加泛型类的日志生成器支持
- 添加了对泛型类的支持,包括类型参数和约束的处理 - 实现了泛型约束的代码生成功能 - 增加了对引用类型、值类型、构造函数约束的支持 - 重构了类声明的生成逻辑以支持泛型参数 - 添加了必要的命名空间引用和集合操作支持
This commit is contained in:
parent
f781be22a9
commit
3fb281031c
@ -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<string>();
|
||||
|
||||
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("// <auto-generated />");
|
||||
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(" /// <summary>Auto-generated logger</summary>");
|
||||
sb.AppendLine(
|
||||
@ -98,6 +124,7 @@ public sealed class LoggerGenerator : TypeAttributeClassGeneratorBase
|
||||
return sb.ToString().TrimEnd();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 可以自定义生成文件名
|
||||
/// </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user