zoukankan      html  css  js  c++  java
  • 设值注入

    namespace Setter
    {
        public interface ITimeProvider
        {
            DateTime CurrentDate { get; }
        }
    
        public class TimeProvider : ITimeProvider
        {
            public DateTime CurrentDate { get { return DateTime.Now; } }
        }
    
        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
        {
            public ITimeProvider TimeProvider { get; set; }
        }
    }

    客户端调用实现

      [TestMethod()]
            public void Test()
            {
                Setter.ITimeProvider timeprovider = (new Setter.Assembler()).Creat<Setter.ITimeProvider>();
                Setter.Client cilent = new Setter.Client();
                cilent.TimeProvider = timeprovider;//设置值注入
              
    
            }

    lama用法

      [TestMethod()]
            public void Test()
            {
              
                //改写lama
                var client = new Setter.Client()
                {
                    TimeProvider = (new Setter.Assembler()).Creat<Setter.ITimeProvider>()
    
                };
    
            }
  • 相关阅读:
    Hive2.0函数大全(中文版)
    Centos7 安装并配置redis
    Java内部类
    beeline: 新版连接Hive server的工具
    jsoup的Document类
    Jsoup类
    jsoup的Node类
    jsoup的Element类
    Java中的多线程
    Java中的文件IO流
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2983432.html
Copyright © 2011-2022 走看看