GFramework/framework/ioc/IocContainer.cs
GwWuYou b7b67e6256 refactor(framework): 重构框架命名空间从GFramework到GWFramework
- 将所有文件中的命名空间GFramework替换为GWFramework
- 更新项目文件GFramework.csproj中的包ID和产品名称为GWFramework
- 修改解决方案文件GFramework.sln中项目的引用名称为GWFramework
- 替换LazyThreadSafetyMode的完整命名空间引用
- 统一调整各模块间相互引用的命名空间前缀
2025-12-09 18:17:22 +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 GWFramework.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;
}
}