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;}}
}
源碼下載