using Microsoft.CodeAnalysis.CSharp.Syntax;
using Serein.Library.Api;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Serein.Library.Utils
{
///
/// 一个轻量级的IOC容器
///
public class SereinIOC : ISereinIOC
{
///
/// 类型集合,暂放待实例化的类型,完成实例化之后移除
///
private readonly ConcurrentDictionary _typeMappings;
///
/// 已完成注入的实例集合
///
private readonly ConcurrentDictionary _dependencies;
///
/// 能够获取类型实例的闭包
///
private readonly ConcurrentDictionary> _registerCallback;
///
/// 未完成注入的实例集合。
/// 键:需要的类型名称
/// 值:元组(对象实例,对象的属性)
///
private readonly ConcurrentDictionary> _unfinishedDependencies;
///
/// IOC容器成功创建了类型
///
public event IOCMembersChangedHandler OnIOCMembersChanged;
///
/// 一个轻量级的IOC容器
///
public SereinIOC()
{
_dependencies = new ConcurrentDictionary();
_registerCallback = new ConcurrentDictionary>();
_typeMappings = new ConcurrentDictionary();
_unfinishedDependencies = new ConcurrentDictionary>();
}
#region 类型的注册
///
/// 向容器注册类型
///
/// 需要注册的类型
///
public ISereinIOC Register(Type type)
{
RegisterType(type.FullName, type);
return this;
}
///
/// 向容器注册类型,并指定其实例成员
///
/// 需要注册的类型
/// 获取实例的回调函数
///
public ISereinIOC Register(Type type, Func