欢迎光临
个人技术文档整理

手写一个及其简易的 IOC 容器

手写一个ioc容器

public class KuContainer : IKuContainer
    {

        private Dictionary<string, Type> _kuContainerDictionary = new Dictionary<string, Type>();

        public void RegisterType<TFrom, TTo>() where TTo : TFrom
        {
            this._kuContainerDictionary.Add(typeof(TFrom).FullName, typeof(TTo));
        }

        public TFrom Resolve<TFrom>()
        {
            var type = this._kuContainerDictionary[typeof(T).FullName];

            var instance = (TFrom)Activator.CreateInstance(type);

            return instance;

        }
    }

22

赞(1)