예제 #1
0
        static void Main(string[] args)
        {
            Driver driver1 = new Driver(new Car());

            driver1.Drive();

            var driver2 = new Driver(new Truck());

            driver2.Drive();

            var driver3 = new Driver(new HeavyTank());

            driver3.Drive();

            Console.ReadKey();

            ITank tank = new HeavyTank();
            //+++++++++++++分割线+++++++++++++++++++++++++++++
            var        t      = tank.GetType();
            object     o      = Activator.CreateInstance(t);
            MethodInfo fireMi = t.GetMethod("Fire");
            MethodInfo runMi  = t.GetMethod("Run");

            fireMi.Invoke(o, null);
            runMi.Invoke(o, null);

            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            var driver = new Driver(new HeavyTank());

            driver.Drive();
            int[]     nums1 = { 1, 2, 3, 4, 5 };
            ArrayList nums2 = new ArrayList {
                1, 2, 3, 4, 5
            };

            Console.WriteLine(Sum(nums1));
            Console.WriteLine(Sum(nums2));

            var roc = new ReadOnlyCollection(nums1);

            Console.WriteLine(Sum(roc));
            //foreach(var n in roc)
            //{
            //    Console.WriteLine(n);
            //}

            IKiller killer = new WarmKiller();

            killer.Kill();
            //var wk =killer as WarmKiller;
            var wk = (IGentleman)killer;

            wk.Love();

            ITank      tank   = new HeavyTank();
            var        t      = tank.GetType();
            object     o      = Activator.CreateInstance(t);
            MethodInfo fireMi = t.GetMethod("Fire");
            MethodInfo runMi  = t.GetMethod("Run");

            fireMi.Invoke(o, null);
            runMi.Invoke(o, null); //reflection



            var sc = new ServiceCollection();

            sc.AddScoped(typeof(ITank), typeof(HeavyTank));
            sc.AddScoped(typeof(IVehicle), typeof(LightTank));
            sc.AddScoped <Driver>();
            var sp      = sc.BuildServiceProvider();
            var driver1 = sp.GetService <Driver>();

            driver1.Drive();

            ITank tank1 = sp.GetService <ITank>();

            tank1.Fire();
            tank1.Run();
        }