2、手写Unity容器

这个场景跟《手写Unity容器--极致简陋版Unity容器》不同,这里构造AndroidPhone的时候,AndroidPhone依赖于1个IPad

思路:

1、RegisterType<TFrom,TTo>()把完整类型名称当作key放入数据字典,把类型当作value放入数据字典方法

2、Resolve<T>(),根据完整类型名称从字典中取出类型

3、得到类型构造函数的参数类型,创建参数类型实例

 

4、最后创建类型实例

1、IPhone接口

namespace SimplestUnity_OneLayer
{
    interface IPhone
    {
        void Call();
    }
}

2、AndroidPhone实现

namespace SimplestUnity_OneLayer
{
    public class AndroidPhone : IPhone
    {
        public AndroidPhone(IPad iPad)
        {
            Console.WriteLine("{0}构造函数", this.GetType().Name);
        }
    
public void Call() { 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、定义1个标记特性DavidInjectionConstructor

 

namespace SimplestUnity_OneLayer
{
    public class DavidInjectionConstructor:Attribute
    {
    }
}

 

6、容器--接口

public interface IDavidContainer
{
    void RegisterType<TFrom, TTo>();
    
    T Resolve<T>();
}

7、容器--实现

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());
        }
    }
}

8、调用

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();
        }
    }
}

 

 

2、手写Unity容器

全文结束