mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(context): 添加服务获取扩展方法
- 实现了从上下文感知对象获取指定类型服务的功能 - 添加了泛型约束确保服务类型为引用类型 - 集成了空值检查以提高代码健壮性 - 提供了详细的XML文档注释说明 - 扩展了IContextAware接口的功能范围
This commit is contained in:
parent
085370aa27
commit
1d7fe998b9
@ -14,6 +14,20 @@ namespace GFramework.Core.extensions;
|
||||
/// </summary>
|
||||
public static class ContextAwareExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 从上下文感知对象中获取指定类型的服务
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">要获取的服务类型</typeparam>
|
||||
/// <param name="contextAware">实现 IContextAware 接口的上下文感知对象</param>
|
||||
/// <returns>指定类型的服务实例,如果未找到则返回 null</returns>
|
||||
/// <exception cref="ArgumentNullException">当 contextAware 参数为 null 时抛出</exception>
|
||||
public static TService? GetService<TService>(this IContextAware contextAware) where TService : class
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(contextAware);
|
||||
var context = contextAware.GetContext();
|
||||
return context.GetService<TService>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构上下文中的指定系统
|
||||
/// </summary>
|
||||
@ -28,6 +42,7 @@ public static class ContextAwareExtensions
|
||||
return context.GetSystem<TSystem>();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构上下文中的指定模型
|
||||
/// </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user