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