From 1d7fe998b998b88bf2a42c54b29a9cdf702ca903 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sun, 1 Feb 2026 13:33:54 +0800
Subject: [PATCH] =?UTF-8?q?feat(context):=20=E6=B7=BB=E5=8A=A0=E6=9C=8D?=
=?UTF-8?q?=E5=8A=A1=E8=8E=B7=E5=8F=96=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 实现了从上下文感知对象获取指定类型服务的功能
- 添加了泛型约束确保服务类型为引用类型
- 集成了空值检查以提高代码健壮性
- 提供了详细的XML文档注释说明
- 扩展了IContextAware接口的功能范围
---
.../extensions/ContextAwareExtensions.cs | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/GFramework.Core/extensions/ContextAwareExtensions.cs b/GFramework.Core/extensions/ContextAwareExtensions.cs
index d21a11a..50785e7 100644
--- a/GFramework.Core/extensions/ContextAwareExtensions.cs
+++ b/GFramework.Core/extensions/ContextAwareExtensions.cs
@@ -14,6 +14,20 @@ namespace GFramework.Core.extensions;
///
public static class ContextAwareExtensions
{
+ ///
+ /// 从上下文感知对象中获取指定类型的服务
+ ///
+ /// 要获取的服务类型
+ /// 实现 IContextAware 接口的上下文感知对象
+ /// 指定类型的服务实例,如果未找到则返回 null
+ /// 当 contextAware 参数为 null 时抛出
+ public static TService? GetService(this IContextAware contextAware) where TService : class
+ {
+ ArgumentNullException.ThrowIfNull(contextAware);
+ var context = contextAware.GetContext();
+ return context.GetService();
+ }
+
///
/// 获取架构上下文中的指定系统
///
@@ -28,6 +42,7 @@ public static class ContextAwareExtensions
return context.GetSystem();
}
+
///
/// 获取架构上下文中的指定模型
///