diff --git a/GFramework.Core/state/AsyncContextAwareStateBase.cs b/GFramework.Core/state/AsyncContextAwareStateBase.cs index 368641e..ff99f21 100644 --- a/GFramework.Core/state/AsyncContextAwareStateBase.cs +++ b/GFramework.Core/state/AsyncContextAwareStateBase.cs @@ -21,7 +21,7 @@ namespace GFramework.Core.state; /// /// 上下文感知异步状态基类 /// 提供基础的异步状态管理功能和架构上下文访问能力 -/// 实现了IAsyncState和IContextAware接口 +/// 实现了IAsyncState(继承IState)和IContextAware接口 /// public class AsyncContextAwareStateBase : IAsyncState, IContextAware, IDisposable { @@ -30,6 +30,49 @@ public class AsyncContextAwareStateBase : IAsyncState, IContextAware, IDisposabl /// private IArchitectureContext? _context; + // ============ IState 同步方法显式实现(隐藏 + 运行时保护) ============ + + /// + /// 同步进入状态(显式实现,不推荐直接调用) + /// 异步状态应该使用 OnEnterAsync 方法 + /// + /// 异步状态不支持同步操作 + [Obsolete("This is an async state. Use OnEnterAsync instead.", error: true)] + void IState.OnEnter(IState? from) + { + throw new NotSupportedException( + $"Async state '{GetType().Name}' does not support synchronous OnEnter. " + + $"Use {nameof(OnEnterAsync)} instead."); + } + + /// + /// 同步退出状态(显式实现,不推荐直接调用) + /// 异步状态应该使用 OnExitAsync 方法 + /// + /// 异步状态不支持同步操作 + [Obsolete("This is an async state. Use OnExitAsync instead.", error: true)] + void IState.OnExit(IState? to) + { + throw new NotSupportedException( + $"Async state '{GetType().Name}' does not support synchronous OnExit. " + + $"Use {nameof(OnExitAsync)} instead."); + } + + /// + /// 同步判断是否可以转换(显式实现,不推荐直接调用) + /// 异步状态应该使用 CanTransitionToAsync 方法 + /// + /// 异步状态不支持同步操作 + [Obsolete("This is an async state. Use CanTransitionToAsync instead.", error: true)] + bool IState.CanTransitionTo(IState target) + { + throw new NotSupportedException( + $"Async state '{GetType().Name}' does not support synchronous CanTransitionTo. " + + $"Use {nameof(CanTransitionToAsync)} instead."); + } + + // ============ IAsyncState 异步方法实现 ============ + /// /// 异步进入状态时调用的方法 /// 子类可重写此方法以实现特定的异步状态进入逻辑(如加载资源、初始化数据等) @@ -61,52 +104,20 @@ public class AsyncContextAwareStateBase : IAsyncState, IContextAware, IDisposabl return Task.FromResult(true); } - /// - /// 同步进入状态(不推荐使用) - /// 异步状态应该使用 OnEnterAsync 方法 - /// - /// 异步状态不支持同步操作 - public virtual void OnEnter(IState? from) - { - throw new NotSupportedException( - $"This is an async state ({GetType().Name}). Use OnEnterAsync instead of OnEnter."); - } - - /// - /// 同步退出状态(不推荐使用) - /// 异步状态应该使用 OnExitAsync 方法 - /// - /// 异步状态不支持同步操作 - public virtual void OnExit(IState? to) - { - throw new NotSupportedException( - $"This is an async state ({GetType().Name}). Use OnExitAsync instead of OnExit."); - } - - /// - /// 同步判断是否可以转换(不推荐使用) - /// 异步状态应该使用 CanTransitionToAsync 方法 - /// - /// 异步状态不支持同步操作 - public virtual bool CanTransitionTo(IState target) - { - throw new NotSupportedException( - $"This is an async state ({GetType().Name}). Use CanTransitionToAsync instead of CanTransitionTo."); - } - /// /// 设置架构上下文 /// /// 架构上下文实例 public void SetContext(IArchitectureContext context) { - _context = context; + _context = context ?? throw new ArgumentNullException(nameof(context)); } /// /// 获取架构上下文 /// /// 架构上下文实例 + /// 当上下文未设置时抛出 public IArchitectureContext GetContext() { return _context ?? throw new InvalidOperationException(