GFramework/GFramework.Core/ioc/IocContainer.cs
GwWuYou e204f899ba refactor(core): 重构框架命名空间为GFramework.Core
- 将所有framework命名空间下的类迁移至GFramework.Core命名空间
- 更新所有相关using引用从framework到Core
- 重命名项目文件夹及文件路径以匹配新的命名空间结构
- 在解决方案中添加GFramework.Core项目引用
- 配置项目依赖关系并移除旧的Generator引用冲突
- 创建独立的GFramework.Core.csproj项目文件支持多目标框架
2025-12-10 08:51:17 +08:00

40 lines
1.0 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.

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;
}
}