Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // 通过接口
            ITest test1 = AopInterceptor.CreateProxyOfInterface <ITest, Test>();
            // 通过类
            Test test2 = AopInterceptor.CreateProxyOfClass <Test>();

            test1.MyMethod();
            test2.MyMethod();

            // 指定构造函数
            test2 = AopInterceptor.CreateProxyOfClass <Test>("aaa", "bbb");
            test2.MyMethod();

            Console.WriteLine("---");

            // 非侵入式代理
            TestNo test3 = AopInterceptor.CreateProxyOfType <TestNo>(
                new ProxyTypeBuilder(new Type[] { typeof(LogAttribute) })
                .AddProxyMethod("LogAttribute", typeof(TestNo).GetMethod(nameof(TestNo.MyMethod)))
                .AddProxyMethod("LogAttribute", typeof(TestNo).GetProperty(nameof(TestNo.A)).GetSetMethod()));

            test3.MyMethod();
            test3.A = "666";

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        //public object CreateClassProxy(Type classToProxy, params IInterceptor[] interceptors);
        public static object ProxyGenerate(Type obj)
        {
            ProxyGenerator generator   = new ProxyGenerator();    //代理类
            AopInterceptor interceptor = new AopInterceptor();    //切面类

            return(generator.CreateClassProxy(obj, interceptor)); //代理进去;
        }
Ejemplo n.º 3
0
        public void CreateProxyOfClass()
        {
            // 通过类
            Test test = AopInterceptor.CreateProxyOfClass <Test>();

            test.MyMethod();
            Assert.True(true);
        }
Ejemplo n.º 4
0
        public void CreateProxyOfInterface()
        {
            // 通过接口
            ITest test = AopInterceptor.CreateProxyOfInterface <ITest, Test>();

            test.MyMethod();
            Assert.True(true);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            //var name = DynamicProxy.GetAssemblyName();
            //var ab = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave);
            //var am = ab.DefineDynamicModule("AOPDebugModule", "AOPDebug.dll");
            //DynamicProxy.SetSave(ab, am);

            //ab.Save("AopDebug.dll");


            Test test = AopInterceptor.CreateProxyOfClass <Test>();

            Console.WriteLine(test.Sum(1, 1));

            Console.ReadKey();
        }
Ejemplo n.º 6
0
        public void TestGenerices()
        {
            // 通过类
            var test = AopInterceptor.CreateProxyOfClass <Testgenerice3 <int, string, ApiCreateProxyTest> >();

            Type[] types  = test.MyTest();
            bool   isType = types[0] == typeof(int) && types[1] == typeof(string) && types[2] == typeof(ApiCreateProxyTest);

            Assert.True(isType);

            var test1 = AopInterceptor.CreateProxyOfClass <Testgenerice3 <object, AopInterceptor, Type> >();

            types  = test1.MyTest();
            isType = types[0] == typeof(int) && types[1] == typeof(string) && types[2] == typeof(ApiCreateProxyTest);

            Assert.True(isType);
        }
Ejemplo n.º 7
0
        public void TestGenerice()
        {
            // 通过类
            var  test  = AopInterceptor.CreateProxyOfClass <Testgenerice1 <int> >();
            bool isInt = test.MyTest() == typeof(int);

            Assert.True(isInt);

            var  test1 = AopInterceptor.CreateProxyOfClass <Testgenerice1 <string> >();
            bool isStr = test1.MyTest() == typeof(string);

            Assert.True(isStr);

            var  test2 = AopInterceptor.CreateProxyOfClass <Testgenerice1 <ApiCreateProxyTest> >();
            bool isobj = test2.MyTest() == typeof(ApiCreateProxyTest);

            Assert.True(isobj);
        }