AOP_03_DispatchProxy

      var s = OwnProxy.Register<IAAAA, AAA>();
              s.Do("111");

            //  IAAAA aAAA = new AAA();

            /// aAAA.Do();
            Console.ReadLine();

        }

        public class OwnProxy : DispatchProxy
        {
            private static  Type _type;
            public static IService Register<IService,Service>()
            {
                _type = typeof(Service);
                return (IService)DispatchProxy.Create<IService, OwnProxy>();
            }
            protected override object Invoke(MethodInfo targetMethod, object[] args)
            {
                // 执行前 
                var actor = Activator.CreateInstance(_type);
                var s = targetMethod.Invoke(actor, args);
                // 执行后 
                return s;
            }
        }
        public interface IAAAA
        {
            void Do(string name);

        }
        public class AAA : IAAAA
        {
            public void Do(string name)
            {
                Console.WriteLine($"{nameof(AAA)} action:{nameof(Do)}");
            }
        }
    }

  

posted @ 2020-04-22 14:06  谁说程序猿很猥琐  阅读(152)  评论(0编辑  收藏  举报