GFramework/GFramework.Core/ioc/IocContainer.cs
GwWuYou 82713e34f0 refactor(core): 重构架构和命令相关代码结构
- 调整了 Architecture 类中字段和方法的布局,提升可读性
- 优化了命令执行逻辑,明确区分有无返回值的命令处理
- 规范了接口和抽象类的注释格式,增强文档清晰度
- 统一了代码风格,对齐缩进与换行符使用
- 补充了事件系统中泛型事件类的功能实现
- 完善了 README 文档中的条目结构和内容表述
2025-12-12 21:10:21 +08:00

36 lines
1016 B
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace GFramework.Core.ioc;
/// <summary>
/// IOC容器类用于管理对象的注册和获取
/// </summary>
public class IocContainer
{
private readonly Dictionary<Type, object> _mInstances = new();
/// <summary>
/// 注册一个实例到IOC容器中
/// </summary>
/// <typeparam name="T">实例的类型</typeparam>
/// <param name="instance">要注册的实例对象</param>
public void Register<T>(T instance)
{
var key = typeof(T);
_mInstances[key] = instance;
}
/// <summary>
/// 从IOC容器中获取指定类型的实例
/// </summary>
/// <typeparam name="T">要获取的实例类型</typeparam>
/// <returns>返回指定类型的实例如果未找到则返回null</returns>
public T Get<T>() where T : class
{
var key = typeof(T);
// 尝试从字典中获取实例
if (_mInstances.TryGetValue(key, out var retInstance)) return retInstance as T;
return null;
}
}