using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;namespace IOC.Common
{public class ZenServiceCollection : IZenServiceCollection{// 记录IOC注册的抽象、实现private Dictionary<string, Type> zenRelationship = new Dictionary<string, Type>();/// <summary>/// IOC容器映射关系注册 ===》 抽象 和具体/// </summary>/// <param name="serviceType">具体类</param>/// <param name="implementtationType">实现类</param>public void AddTransient(Type serviceType, Type implementtationType){this.zenRelationship.Add(serviceType.FullName, implementtationType);}/// <summary>/// 获取服务/// </summary>/// <typeparam name="T"></typeparam>/// <returns></returns>public T GetService<T>(){{//只限有无参构造函数, 若类没有构造函数,ctr会自动生成一个无参构造函数,但若定义了有参构造函数,就不会自动创建无参构造函数啦//Type t = zenRelationship[typeof(T).FullName];//return (T)Activator.CreateInstance(t);}{继续迭代 支持有参构造函数 ,只支持1个层级的有参构造函数 //Type t = zenRelationship[typeof(T).FullName];确定构造当前对象使用哪个构造函数(默认选择参数最多的构造函数)//ConstructorInfo[] ctors = t.GetConstructors();//ConstructorInfo ctor = ctors.OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();//List<object> paralist = new List<object>();//foreach (ParameterInfo item in ctor.GetParameters())//{// Type tt = item.ParameterType;// Type ttt = item.GetType();// Type t1 = zenRelationship[item.ParameterType.FullName];// var target = Activator.CreateInstance(t1);// paralist.Add(target);//}//return (T)Activator.CreateInstance(t, paralist.ToArray());}{继续迭代 支持有参构造函数 无线层级的//Type t = zenRelationship[typeof(T).FullName];//return (T)this.GetService(t); }{//利用特性 指明构造函数创建 , 默认是 使用 参数最多Type t = zenRelationship[typeof(T).FullName];return (T)this.GetService(t);}}private object GetService(Type type){#region 构造函数注入ConstructorInfo[] ctors = type.GetConstructors();ConstructorInfo ctor = ctors.Where(c=>c.IsDefined(typeof(SelectConstructAttribute),true)).FirstOrDefault();if (ctor==null){//当ctor不存在,则表示 构造函数没有对应特性标识ctor= ctors.OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();}List<object> paralist = new List<object>(); foreach (ParameterInfo item in ctor.GetParameters()){Type t1 = zenRelationship[item.ParameterType.FullName];var target=this.GetService(t1); paralist.Add(target);}#region 方法注入//构造对象object objInstance = Activator.CreateInstance(type, paralist.ToArray()); //获取对象的属性 只获取含有特性PropertyInjectionAttribute的属性foreach (MethodInfo item in type.GetMethods().Where(c => c.IsDefined(typeof(MethodInjectionAttribute), true))){List<object> paraOfMethodlist = new List<object>();foreach (ParameterInfo para in item.GetParameters()){Type t1 = zenRelationship[para.ParameterType.FullName];var target = this.GetService(t1);paraOfMethodlist.Add(target);}item.Invoke(objInstance, paraOfMethodlist.ToArray());}#endregion#endregion#region 属性注入 //获取对象的属性 只获取含有特性PropertyInjectionAttribute的属性foreach (PropertyInfo item in type.GetProperties().Where(c=>c.IsDefined(typeof(PropertyInjectionAttribute),true))){Type t1 = zenRelationship[item.PropertyType.FullName];var target = this.GetService(t1);item.SetValue(objInstance,target);}#endregionreturn objInstance;}}
}
源码下载