Example #1
0
        static void Main(string[] args)
        {
            var robot1 = SuperIoc.GetInstanceDAL <IRobot>();

            robot1.Move(100);

            var robot2 = SuperIoc.GetInstance().Container.Resolve <IRobot>();

            robot2.Move(200);

            var controller = SuperIoc.GetInstanceDAL <DeviceController>();

            controller.DoWork(300);
            controller.Robot.Move(400);

            // 在Unity配置文件中,如果设置了lifetime类型为singleton
            // 则以下结果均为true,否则均为false
            bool b1 = robot1.Equals(robot2);
            bool b2 = robot1.Equals(controller.Robot);

            Console.WriteLine($"robot1 Equals robot2 : {b1}");
            Console.WriteLine($"robot1 Equals robot3 : {b2}");

            Console.ReadKey();
        }
Example #2
0
        /// <summary>
        /// 单例,静态方法
        /// </summary>
        public static SuperIoc GetInstance()
        {
            if (instance == null || instance.Container == null)
            {
                instance = new SuperIoc()
                {
                    Container = GetContainer()
                };
            }

            return(instance);
        }