Exemplo n.º 1
0
        /// <summary>
        /// 创建一个经过Aop和Ioc处理的对象,结果不是单例。
        /// 如果需要拦截,则创建代理子类;然后检测是否需要注入。
        /// </summary>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public static Object CreateObject(Type targetType)
        {
            Object objTarget = AopContext.CreateObjectBySub(targetType);

            Inject(objTarget);
            return(objTarget);
        }
Exemplo n.º 2
0
        public void testSubclassObject()
        {
            // 不管有没有监控,CreateObject 都返回结果
            MyNormalService s3 = AopContext.CreateObjectBySub <MyNormalService>();

            Assert.IsNotNull(s3);
            Assert.AreEqual(typeof(MyNormalService), s3.GetType());
        }
Exemplo n.º 3
0
        /// <summary>
        /// 创建一个经过Aop和Ioc处理的对象,结果不是单例。
        /// 如果需要拦截,则创建代理子类;然后检测是否需要注入。
        /// </summary>
        /// <param name="targetType"></param>
        /// <param name="invokerName">如果是根据接口自动装配,</param>
        /// <returns></returns>
        private static Object CreateObject(Type targetType, Object invoker)
        {
            if (targetType == null)
            {
                return(null);
            }

            if (targetType.IsInterface)
            {
                return(CreateByInterface(targetType, invoker));
            }

            Object objTarget = AopContext.CreateObjectBySub(targetType);

            Inject(objTarget);
            return(objTarget);
        }
Exemplo n.º 4
0
        public void testInterfaceClassProxy()
        {
            // 代理类是接口
            IMyAopService s1 = AopContext.CreateProxyByInterface <IMyAopService>(typeof(MyAopService));

            Assert.AreNotEqual(typeof(MyAopService), s1.GetType());

            s1.Save();
            Console.WriteLine();
            Console.WriteLine();

            // 未修改结果
            int result1 = s1.Update(88);

            Assert.AreEqual(MyAopService.UpdateResult, result1);
            Console.WriteLine();
            Console.WriteLine();

            s1.GetBy("myname", 3);
            Console.WriteLine();
            Console.WriteLine();

            // 边界测试:null
            List <String> result2 = s1.GetCat();

            Assert.IsNull(result2);
            Console.WriteLine();
            Console.WriteLine();

            // 泛型列表
            List <String> result3 = s1.GetDog();

            Assert.IsNotNull(result3);
            Assert.AreEqual(0, result3.Count);
            Console.WriteLine();
            Console.WriteLine();

            // 后置修改:修改结果数据
            List <MyCat> result4 = s1.GetCat2();

            Assert.IsNotNull(result4);
            Assert.AreEqual(4, result4.Count);
            Assert.AreEqual("cat999", result4[3].Name);
            Console.WriteLine();
            Console.WriteLine();

            // 未监控的方法
            s1.NormalMethod();
            Console.WriteLine();
            Console.WriteLine();

            // 未监控的虚方法
            s1.NormalVirtualMethod();
            Console.WriteLine();
            Console.WriteLine();

            // 没有监控的对象,则没有代理类
            MyNormalService s2 = AopContext.CreateProxyBySub <MyNormalService>();

            Assert.IsNull(s2);

            // 不管有没有监控,CreateObject 都返回结果
            MyNormalService s3 = AopContext.CreateObjectBySub <MyNormalService>();

            Assert.IsNotNull(s3);
            Assert.AreEqual(typeof(MyNormalService), s3.GetType());

            // 测试参数是否成功修改
            MyArgService x1 = AopContext.CreateProxyBySub <MyArgService>();

            Console.WriteLine("buy1");
            int xResult1 = x1.Buy(3);

            Console.WriteLine();

            Console.WriteLine("buy2");
            int xResult2 = x1.Buy(5);

            Assert.AreEqual(8, xResult1);
            Assert.AreEqual(12, xResult2);
        }