2、手写Unity容器--第一层依赖注入
作者:互联网
这个场景跟《手写Unity容器--极致简陋版Unity容器》不同,这里构造AndroidPhone的时候,AndroidPhone依赖于1个IPad
1、IPhone接口
namespace SimplestUnity_OneLayer { interface IPhone { void Call(); } }
2、AndroidPhone实现
namespace SimplestUnity_OneLayer { public class AndroidPhone : IPhone { public AndroidPhone() { Console.WriteLine("{0}构造函数", this.GetType().Name); } } }
3、IPad接口
namespace SimplestUnity_OneLayer { public interface IPad { void Show(); } }
4、IPad实现
namespace SimplestUnity_OneLayer { public class AndroidPad:IPad { public AndroidPad() { Console.WriteLine("{0}构造函数", this.GetType().Name); } public void Show() { Console.WriteLine("看{0}", this.GetType().Name); } } }
5、容器--接口
public interface IDavidContainer { void RegisterType<TFrom, TTo>(); T Resolve<T>(); }
6、容器--实现
namespace SimplestUnity_OneLayer { /// <summary> /// 容器--工厂 /// </summary> public class DaivdContainer:IDaivdContainer { private Dictionary<string, Type> containerDictionary = new Dictionary<string, Type>();//字典 /// <summary> /// 注册类型 /// </summary> /// <typeparam name="TFrom"></typeparam> /// <typeparam name="TTo"></typeparam> public void RegisterType<TFrom, TTo>() { containerDictionary.Add(typeof(TFrom).FullName, typeof(TTo)); } /// <summary> /// 获取实例 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T Resolve<T>() { Type type = containerDictionary[typeof(T).FullName]; //1、得到类型的所有构造函数 ConstructorInfo[] ctorArray = type.GetConstructors(); //2、只得到有标记DavidInjectionConstructor特性的构造函数,如果都没有标记特性,那么得到参数最多的构造函数 ConstructorInfo currentCtor = null; if (ctorArray.Count(c => c.IsDefined(typeof(DavidInjectionConstructor), true)) > 0) { currentCtor = ctorArray.FirstOrDefault(c => c.IsDefined(typeof(DavidInjectionConstructor), true));//得到第1个标记DavidInjectionConstructor特性的构造函数 } else { currentCtor = ctorArray.OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();//得到参数个数最多的构造函数 } List<object> paraList = new List<object>(); foreach (var para in currentCtor.GetParameters()) { //得到的参数类型是IPad,抽象无法创建实例 var paraType = para.ParameterType; //所以根据IPad Key,得到AndroidPad类型,具体类型就可以创建实例 var targetParaType = containerDictionary[paraType.FullName]; paraList.Add(Activator.CreateInstance(targetParaType)); } return (T)Activator.CreateInstance(type,paraList.ToArray()); } } }
7、调用
namespace SimplestUnity_OneLayer { class Program { static void Main(string[] args) { DaivdContainer davidContainer = new DaivdContainer(); davidContainer.RegisterType<IPhone, AndroidPhone>(); davidContainer.RegisterType<IPad, AndroidPad>(); IPhone iphone = davidContainer.Resolve<IPhone>(); iphone.Call(); } } }
标签:IPad,OneLayer,void,SimplestUnity,第一层,Unity,手写,public,构造函数 来源: https://www.cnblogs.com/menglin2010/p/12080179.html