diff --git a/GFramework.Core.SourceGenerators/Rule/ContextAwareGenerator.cs b/GFramework.Core.SourceGenerators/Rule/ContextAwareGenerator.cs
index 5e3a0356..65919b25 100644
--- a/GFramework.Core.SourceGenerators/Rule/ContextAwareGenerator.cs
+++ b/GFramework.Core.SourceGenerators/Rule/ContextAwareGenerator.cs
@@ -107,7 +107,7 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine(
"/// 已缓存的实例上下文需要通过 显式覆盖。");
sb.AppendLine(
- "/// 与手动继承 的路径相比,生成实现会使用 _contextSync 协调惰性初始化、provider 切换和显式上下文注入;");
+ "/// 与手动继承 的路径相比,生成实现会使用 _gFrameworkContextAwareSync 协调惰性初始化、provider 切换和显式上下文注入;");
sb.AppendLine(
"/// 则保持无锁的实例级缓存语义,更适合已经由调用方线程模型保证串行访问的简单场景。");
sb.AppendLine("/// ");
@@ -151,10 +151,11 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
/// 字符串构建器。
private static void GenerateContextBackingFields(StringBuilder sb)
{
- sb.AppendLine(" private global::GFramework.Core.Abstractions.Architectures.IArchitectureContext? _context;");
sb.AppendLine(
- " private static global::GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider? _contextProvider;");
- sb.AppendLine(" private static readonly object _contextSync = new();");
+ " private global::GFramework.Core.Abstractions.Architectures.IArchitectureContext? _gFrameworkContextAwareContext;");
+ sb.AppendLine(
+ " private static global::GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider? _gFrameworkContextAwareProvider;");
+ sb.AppendLine(" private static readonly object _gFrameworkContextAwareSync = new();");
sb.AppendLine();
}
@@ -177,7 +178,7 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine(
" /// 或 不会自动清除此缓存;如需覆盖,请显式调用 IContextAware.SetContext(...)。");
sb.AppendLine(
- " /// 当前实现还假设 可在持有 _contextSync 时安全执行;");
+ " /// 当前实现还假设 可在持有 _gFrameworkContextAwareSync 时安全执行;");
sb.AppendLine(
" /// 自定义 provider 不应在该调用链内重新进入当前类型的 provider 配置 API,且应避免引入与外部全局锁相互等待的锁顺序。");
sb.AppendLine(" /// ");
@@ -185,7 +186,7 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine(" {");
sb.AppendLine(" get");
sb.AppendLine(" {");
- sb.AppendLine(" var context = _context;");
+ sb.AppendLine(" var context = _gFrameworkContextAwareContext;");
sb.AppendLine(" if (context is not null)");
sb.AppendLine(" {");
sb.AppendLine(" return context;");
@@ -193,13 +194,13 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine();
sb.AppendLine(" // 在同一个同步域内协调懒加载与 provider 切换,避免读取到被并发重置的空提供者。");
sb.AppendLine(
- " // provider 的 GetContext() 会在持有 _contextSync 时执行;自定义 provider 必须避免在该调用链内回调 SetContextProvider/ResetContextProvider 或形成反向锁顺序。");
- sb.AppendLine(" lock (_contextSync)");
+ " // provider 的 GetContext() 会在持有 _gFrameworkContextAwareSync 时执行;自定义 provider 必须避免在该调用链内回调 SetContextProvider/ResetContextProvider 或形成反向锁顺序。");
+ sb.AppendLine(" lock (_gFrameworkContextAwareSync)");
sb.AppendLine(" {");
sb.AppendLine(
- " _contextProvider ??= new global::GFramework.Core.Architectures.GameContextProvider();");
- sb.AppendLine(" _context ??= _contextProvider.GetContext();");
- sb.AppendLine(" return _context;");
+ " _gFrameworkContextAwareProvider ??= new global::GFramework.Core.Architectures.GameContextProvider();");
+ sb.AppendLine(" _gFrameworkContextAwareContext ??= _gFrameworkContextAwareProvider.GetContext();");
+ sb.AppendLine(" return _gFrameworkContextAwareContext;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" }");
@@ -216,6 +217,8 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine(" /// 配置当前生成类型共享的上下文提供者。");
sb.AppendLine(" /// ");
sb.AppendLine(" /// 后续懒加载上下文时要使用的提供者实例。");
+ sb.AppendLine(
+ " /// 当 为 null 时抛出。");
sb.AppendLine(" /// ");
sb.AppendLine(" /// 该方法使用与 相同的同步锁,避免提供者切换与惰性初始化交错。");
sb.AppendLine(
@@ -225,9 +228,10 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine(
" public static void SetContextProvider(global::GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider provider)");
sb.AppendLine(" {");
- sb.AppendLine(" lock (_contextSync)");
+ sb.AppendLine(" global::System.ArgumentNullException.ThrowIfNull(provider);");
+ sb.AppendLine(" lock (_gFrameworkContextAwareSync)");
sb.AppendLine(" {");
- sb.AppendLine(" _contextProvider = provider;");
+ sb.AppendLine(" _gFrameworkContextAwareProvider = provider;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine();
@@ -242,9 +246,9 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine(" /// ");
sb.AppendLine(" public static void ResetContextProvider()");
sb.AppendLine(" {");
- sb.AppendLine(" lock (_contextSync)");
+ sb.AppendLine(" lock (_gFrameworkContextAwareSync)");
sb.AppendLine(" {");
- sb.AppendLine(" _contextProvider = null;");
+ sb.AppendLine(" _gFrameworkContextAwareProvider = null;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine();
@@ -316,9 +320,9 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
{
case "SetContext":
sb.AppendLine(" // 与 Context getter 共享同一同步协议,避免显式注入被并发懒加载覆盖。");
- sb.AppendLine(" lock (_contextSync)");
+ sb.AppendLine(" lock (_gFrameworkContextAwareSync)");
sb.AppendLine(" {");
- sb.AppendLine(" _context = context;");
+ sb.AppendLine(" _gFrameworkContextAwareContext = context;");
sb.AppendLine(" }");
break;
diff --git a/GFramework.Core/Functional/Option.cs b/GFramework.Core/Functional/Option.cs
index 31d18d2e..4e738625 100644
--- a/GFramework.Core/Functional/Option.cs
+++ b/GFramework.Core/Functional/Option.cs
@@ -16,6 +16,21 @@ namespace GFramework.Core.Functional;
///
/// 表示可能存在或不存在的值,用于替代 null 引用的函数式编程类型
///
+///
+///
+/// 只表示两种显式状态:通过 创建的有值状态,以及
+/// 表示的无值状态;调用方不应把 当作 的别名。
+///
+///
+/// 会拒绝 ,因此引用类型和可空引用类型参数都必须包装真实值;访问方应优先通过
+/// 、、模式匹配或 Match/Map 等函数式 API 消费结果,而不是假设默认值
+/// 与无值状态等价。
+///
+///
+/// 该结构体是不可变值类型;一旦创建,其状态与内部值不会再改变。但在 为 时,
+/// 调用需要真实值的方法仍应遵守各成员自身的契约与异常说明。
+///
+///
/// 值的类型
public readonly struct Option : IEquatable