Example #1
0
        /// <summary>
        /// 静态方法重写测试
        /// </summary>
        static void Static()
        {
            Console.WriteLine("Static Start");
            using (methodWriter <Func <int> > methodWriter = new methodWriter <Func <int> >(destination.Get1))
            {
                Console.WriteLine("原始调用输出1 -> " + destination.Get1().toString());
                Console.WriteLine("静态方法原始调用输出1 -> " + methodWriter.Method().toString());

                methodWriter.Set(new source().Get2);
                Console.WriteLine("重写到实例方法source.Get2后调用输出2 -> " + destination.Get1().toString());

                methodWriter.Set(source.Get3);
                Console.WriteLine("重写到静态方法source.Get3后调用输出3 -> " + destination.Get1().toString());
            }
            Console.WriteLine("释放重写后原始调用输出1 -> " + destination.Get1().toString());
            Console.WriteLine("Static End");
        }
Example #2
0
        /// <summary>
        /// 静态属性重写测试
        /// </summary>
        static void StaticProperty()
        {
            Console.WriteLine("StaticProperty Start");
            destination destination = new destination {
                Value = 1
            };

            using (methodWriter <Func <int> > methodWriter = new methodWriter <Func <int> >((Func <int>)Delegate.CreateDelegate(typeof(Func <int>), typeof(destination).GetProperty("StaticProperty", BindingFlags.Static | BindingFlags.Public).GetGetMethod(false))))
            {
                Console.WriteLine("原始调用输出1 -> " + destination.StaticProperty.toString());
                Console.WriteLine("静态方法原始调用输出1 -> " + methodWriter.Method().toString());

                methodWriter.Set(new source().Get2);
                Console.WriteLine("重写到实例方法source.Get2后调用输出2 -> " + destination.StaticProperty.toString());

                methodWriter.Set(source.Get3);
                Console.WriteLine("重写到静态方法source.Get3后调用输出3 -> " + destination.StaticProperty.toString());
            }
            Console.WriteLine("释放重写后原始调用输出1 -> " + destination.StaticProperty.toString());
            Console.WriteLine("StaticProperty End");
        }
Example #3
0
        /// <summary>
        /// 实例方法重写测试
        /// </summary>
        static void Instance()
        {
            Console.WriteLine("Instance Start");
            destination destination = new destination {
                Value = 1
            };

            using (methodWriter <Func <int> > methodWriter = new methodWriter <Func <int> >(destination.Get))
            {
                Console.WriteLine("原始调用输出1 -> " + destination.Get().toString());
                Console.WriteLine("没有绑定实例的实例方法调用输出“随机数” -> " + methodWriter.Method().toString());

                methodWriter.Set(new source().Get2);
                Console.WriteLine("重写到实例方法source.Get2后调用输出2 -> " + destination.Get().toString());

                methodWriter.Set(source.Get3);
                Console.WriteLine("重写到静态方法source.Get3后调用输出3 -> " + destination.Get().toString());
            }
            Console.WriteLine("释放重写后原始调用输出1 -> " + destination.Get().toString());
            Console.WriteLine("Instance End");
        }