zoukankan      html  css  js  c++  java
  • 接口注入

    public interface ITimeProvider
        {
            DateTime CurrentDate { get; }
        }
        public interface IobjectWithTimeProvider
        {
            ITimeProvider TimeProvider { get; set; }
        }
        public class TimeProvider : ITimeProvider
        {
            public DateTime CurrentDate { get { return DateTime.Now; } }
        }
    
        //下面实现这个Assembler
        public class Assembler
        {
            static Dictionary<Type, Type> dictionary = new Dictionary<Type, Type>();
            static Assembler()
            {
                dictionary.Add(typeof(ITimeProvider), typeof(TimeProvider));
            }
    
            public object Creat(Type type)
            {
                if ((type == null) || !dictionary.ContainsKey(type))
                    throw new NullReferenceException();
                return Activator.CreateInstance(dictionary[type]);
            }
    
            //泛型方式调用
            public T Creat<T>()
            {
                return (T)Creat(typeof(T));
            }
        }
    
        public class Client : IobjectWithTimeProvider
        {
           public ITimeProvider TimeProvider { get; set; }
        }
     [TestClass()]
        public class ClientTest
        {
    
            [TestMethod]
            public void Test()
            {
                ITimeProvider timeprovider = (new Assembler()).Creat<ITimeProvider>();
                IobjectWithTimeProvider objectprovider = new Client();
                objectprovider.TimeProvider = timeprovider;
            }
           
        }

    与值注入有点类似,客户端去实现这个接口,这样客户端就可以获得类型,然后通过Assembler实现再通过赋值的形式注入

  • 相关阅读:
    tushare包使用案例
    Matplotlib模块:绘图和可视化
    pandas使用
    django 表操作
    元数据Meta
    django关系类型字段
    django项目模型字段
    django项目mysite 2
    django安装使用xadmin
    GCC版本中没有GLIBCXX_3.4.15错误
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2983458.html
Copyright © 2011-2022 走看看