GFramework/framework/ioc/IocContainer.cs
GwWuYou 5aa11ddc41 feat(architecture): 添加架构核心组件与命令模式实现
- 新增 Architecture 基类与 IArchitecture 接口,实现单例模式与组件注册管理
- 集成 IOC 容器支持系统、模型、工具的依赖注入与生命周期管理
- 实现命令模式基础类 AbstractCommand 与接口 ICommand,支持带返回值命令
- 提供事件系统集成,支持事件的发布与订阅机制
- 添加控制器接口 IController,整合命令发送、事件注册与模型获取能力
- 创建详细的 README 文档说明各组件使用方式与设计模式应用
- 支持命令、查询、事件的统一调度与解耦通信机制
2025-12-09 15:32:17 +08:00

43 lines
1.1 KiB
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.

using System;
using System.Collections.Generic;
namespace GFramework.framework.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;
}
}