Example #1
0
        public void Show()
        {
            NoReturnNoPara noReturnNoPara = new NoReturnNoPara(DoNothing); //2 委托的实例化

            noReturnNoPara.Invoke();                                       //3 委托实例的调用

            {
                WithReturnWithPara withReturnWithPara = new WithReturnWithPara(this.DoNothing2);
                string             result             = withReturnWithPara.Invoke(2);
                Console.WriteLine(result);
            }
            {
                //多播委托:一个变量保存多个方法,可以增减;invoke的时候可以按顺序执行
                //+= 为委托实例按顺序增加方法,形成方法链,Invoke时,按顺序依次执行
                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(DoNothingStatic);
                method.Invoke();

                foreach (NoReturnNoPara item in method.GetInvocationList())
                {
                    item.Invoke();
                }

                //-= 为委托实例移除方法,从方法链的尾部开始匹配,遇到第一个完全吻合的,移除且只移除一个,没有也不异常
                method -= new NoReturnNoPara(this.DoNothing);
                method -= new NoReturnNoPara(DoNothingStatic);
                method.Invoke();
            }
        }
Example #2
0
        public void Show()
        {
            Student student = new Student()
            {
                Id      = 123,
                Name    = "JASON",
                Age     = 32,
                ClassId = 1
            };

            student.Study();
            {
                //NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
                //method.Invoke(); //Execute DoNothing method.
                //method(); // Same as method.Invoke()
            }

            //Multicast delegate: a variable holds multiple methods, which can add or subtract.
            //The invoke can be executed sequentially
            //+= adds methods to the delegate instance in order to form the method chain, and when Invoke, it executes in order
            //Milticast delegate could not be async.
            Student studentNew = new Student();

            Console.WriteLine("+=");
            NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);//No () for argument method

            method += new NoReturnNoPara(DoNothingStatic);
            method += new NoReturnNoPara(studentNew.Study);
            method += new NoReturnNoPara(Student.StudyAdvanced);
            method += new NoReturnNoPara(this.DoNothing);

            method.Invoke();

            Console.WriteLine("-=");
            //-= removes a method for the delegate instance, matches it from the end of the method chain
            //, and when the first one is fully matched, removes and removes only one, no and no exceptions
            method -= new NoReturnNoPara(DoNothing);
            method -= new NoReturnNoPara(studentNew.Study);

            //Go throught a methods list
            foreach (NoReturnNoPara item in method.GetInvocationList())
            {
                item.Invoke();
            }

            method.Invoke();
        }
        public void Show()
        {
            //{
            //    WithReturnNoPara method1 = new WithReturnNoPara(ToInt);
            //    int iResult = method1.Invoke();
            //}

            //Student student = new Student()
            //{
            //    Id = 1,
            //    Name = "周瑾",
            //    Age = 25
            //};
            //student.Study();

            //委托的实例化:要求传递一个和委托参数返回值完全一致的方法。
            NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
            //NoReturnNoPara method1 = new NoReturnNoPara(this.ParaReturn);
            //method.Invoke();// 就是去调用方法
            //method();// 可以省略Invoke
            //this.DoNothing(); //和委托Invoke 是做的同一件事
            //method.BeginInvoke(null, null);//这里是开启一个新的线程去执行方法
            //method.EndInvoke(null);

            //1、声明一个委托
            //2、委托的实例化
            //3、调用

            // 系统内置的委托
            {
                //Action  Func  从.Netframework 3.0出现,是系统提供的委托
                //Action:没有返回值, 参数可有可无
                //Func: 必须有返回值,参数可有可无
                //我们其实可以自定义委托呀,那系统为什么还要定义这两个委托呢?

                //new Thread(new ThreadStart(DoNothing));
                //new Thread(new NoReturnNoPara(DoNothing)); // 是不是重复声明?
                ////我们的思维来讲:应该是不会报错
                //这里为什么不行呢? 是因为委托是一个类,两个实例肯定不一样啊!

                //所以系统提供Action Func 两个委托,系统肯定是希望在以后的封装中能够直接使用这两个委托!

                //Action: 没有返回值,最多支持16个泛型参数
                //Action act = new Action(this.DoNothing);
                //Action<int> actInt = new Action<int>(NoReturn);
                //Action<int, string, DateTime, object, int, string, DateTime, object, int, string, DateTime, object, int, string, DateTime, object> action = null;

                ////Func: 有返回值,最多支持16个泛型参数,泛型参数列表的最后一个为委托返回值类型
                //Func<int> func1 = new Func<int>(ToInt);
                //Func<int, int, string, DateTime, object, int, string, DateTime, object, int, string, DateTime, object, int, string, DateTime, object> Func2 = null;
                //int iResult = func1.Invoke();
            }

            {//多播委托 我们自定义的任何一个委托都是多播委托
             //可以通过+=/-=  向委托中增加或者移除方法;
             //+=增加方法以后,让委托组成了一个方法链

                //-=只能移除同一个实例里的方法,如过没有可以移除的方法,就啥事儿也不发生
                NoReturnNoPara method1 = new NoReturnNoPara(this.DoNothing);

                var student = new Student();

                method1 += this.DoNothing;
                method1 += student.Study; //加括号就是调用

                method1 -= this.DoNothing;
                method1 -= student.Study; //仍然会执行 因为是两个不同的实例
                method1.Invoke();

                foreach (NoReturnNoPara item in method1.GetInvocationList())
                {
                    item.Invoke();
                }

                // 如果func 委托在+= 形成一个方法链以后,在执行后,只能获取最后一个方法的返回值
            }

            //现在是21:55  大家开始提问,21:58开始答疑,期间老师不说话
        }
Example #4
0
        public void Show()
        {
            //System.MulticastDelegate
            Student student = new Student()
            {
                Id      = 123,
                Name    = "靠谱一大叔",
                Age     = 32,
                ClassId = 1
            };

            student.Study();
            {
                //把方法包装成对象,invoke的时候自动执行方法
                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); //2 委托的实例化
                method.Invoke();                                            //3 委托实例的调用
                method();

                this.DoNothing();
            }
            //begininvoke
            {
                WithReturnNoPara method = new WithReturnNoPara(this.GetSomething);
                int iResult             = method.Invoke();
                iResult = method();
                var result = method.BeginInvoke(null, null);//异步调用
                method.EndInvoke(result);
            }
            {
                //多种途径实例化
                {
                    NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
                }
                {
                    NoReturnNoPara method = new NoReturnNoPara(DoNothingStatic);
                }
                {
                    NoReturnNoPara method = new NoReturnNoPara(Student.StudyAdvanced);
                }
                {
                    NoReturnNoPara method = new NoReturnNoPara(new Student().Study);
                }
            }

            {
                //多播委托:一个变量保存多个方法,可以增减;invoke的时候可以按顺序执行
                //+= 为委托实例按顺序增加方法,形成方法链,Invoke时,按顺序依次执行
                Student studentNew = new Student();

                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(DoNothingStatic);
                method += new NoReturnNoPara(Student.StudyAdvanced);
                method += new NoReturnNoPara(new Student().Study);//不是同一个实例,所以是不同的方法
                method += new NoReturnNoPara(studentNew.Study);
                method.Invoke();

                //method.BeginInvoke(null, null);//多播委托是不能异步的
                foreach (NoReturnNoPara item in method.GetInvocationList())
                {
                    item.Invoke();
                    //item.BeginInvoke(null, null);
                }
                //-= 为委托实例移除方法,从方法链的尾部开始匹配,遇到第一个完全吻合的,移除且只移除一个,没有也不异常
                method -= new NoReturnNoPara(this.DoNothing);
                method -= new NoReturnNoPara(DoNothingStatic);
                method -= new NoReturnNoPara(Student.StudyAdvanced);
                method -= new NoReturnNoPara(new Student().Study);
                method -= new NoReturnNoPara(studentNew.Study);
                method.Invoke();
            }
            {
                WithReturnNoPara method = new WithReturnNoPara(this.GetSomething);
                method += new WithReturnNoPara(this.GetSomething2);
                method += new WithReturnNoPara(this.GetSomething3);
                int iResult = method.Invoke();//多播委托带返回值,结果以最后的为准
            }
        }
Example #5
0
        public void Show()
        {
            {
                NoReturnWithPara func = (x, y) => { };
                func += (x, y) => Console.WriteLine("123");;

                func.Invoke(3, 4);
            }



            //System.MulticastDelegate
            Student student = new Student()
            {
                Id   = 96,
                Name = "Summer"
            };

            student.Study();

            //Predicate<int>

            //Func<string>

            {
                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);//2 委托的实例化


                method.Invoke();//3 委托的调用
                method();

                //method.BeginInvoke(null, null);

                this.DoNothing();
            }

            {
                NoReturnNoPara method  = new NoReturnNoPara(this.DoNothing);             //当前类的实例方法
                NoReturnNoPara method1 = new NoReturnNoPara(MyDelegate.DoNothingStatic); //当前类的静态方法
                NoReturnNoPara method2 = new NoReturnNoPara(student.Study);              //其他类的实例方法
                NoReturnNoPara method3 = new NoReturnNoPara(Student.StudyAdvanced);      //其他类的静态方法
            }

            {
                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(MyDelegate.DoNothingStatic);
                method += new NoReturnNoPara(student.Study);
                method += new NoReturnNoPara(Student.StudyAdvanced);

                method += new NoReturnNoPara(new Student().Study);
                method += new NoReturnNoPara(Student.StudyAdvanced);


                method += new NoReturnNoPara(() => Console.WriteLine("这里是lambda表达式"));
                //+=就是把多个方法按顺序排成列表,invoke时按顺序执行
                //method.Invoke();

                method -= new NoReturnNoPara(this.DoNothing);
                method -= new NoReturnNoPara(MyDelegate.DoNothingStatic);
                method -= new NoReturnNoPara(student.Study);

                method -= new NoReturnNoPara(new Student().Study);
                method -= new NoReturnNoPara(Student.StudyAdvanced);

                method -= new NoReturnNoPara(Student.StudyAdvanced);
                method -= new NoReturnNoPara(() => Console.WriteLine("这里是lambda表达式"));
                //-=就是从这个实例上,从后往前挨个匹配,找到第一个完全吻合的移除掉,且只移除一个,找不到不异常
                method.Invoke();


                //method.BeginInvoke(null, null);
                foreach (NoReturnNoPara item in method.GetInvocationList())
                {
                    item.BeginInvoke(null, null);
                }
            }
        }
Example #6
0
        public void Show()
        {
            Student student = new Student()
            {
                Id      = 123,
                Name    = "Ivan",
                Age     = 32,
                ClassId = 1
            };

            student.Study();

            {
                //把方法包装成变量, invoke 的时候自动执行方法
                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); // 2. 委托的实例化, 要求传入参数,参数为一个函数,函数必须与委托有同样的函数签名及返回值

                {
                    //3. 委托实例的调用, 相当于把传入的函数执行一遍
                    method.Invoke(); // Invoke 是delegate IL 中的方法, 相当于调用类的实例方法
                    method();        // 也可以直接省略Invoke进行调用
                }
            }

            {
                // beginInvoke
                WithReturnNoPara method = new WithReturnNoPara(this.GetSomething);
                int iResult             = method.Invoke();
                iResult = method();

                var result = method.BeginInvoke(null, null);// 异步调用
                method.EndInvoke(result);
            }
            {
                // 多途径实例化

                {
                    NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
                }
                {
                    NoReturnNoPara method = new NoReturnNoPara(DoNothingStatic);
                }
                {
                    NoReturnNoPara method = new NoReturnNoPara(Student.StudyAdvanced);
                }
                {
                    NoReturnNoPara method = new NoReturnNoPara(new Student().Study);
                }
            }
            {
                // 多播委托: 一个变量保存多个方法,可以增减;Invoke时候可以按顺序执行
                // += 为委托实例按顺序增加方法,形成方法链, Invoke时,是按顺序依次执行
                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(this.DoNothing);
                method += new NoReturnNoPara(DoNothingStatic);
                method += new NoReturnNoPara(Student.StudyAdvanced);
                method += new NoReturnNoPara(new Student().Study);// 不是同一个实例, 所以是不同的方法
                method += new NoReturnNoPara(student.Study);
                method.Invoke();


                method.BeginInvoke(null, null); // 多播委托不能异步

                foreach (NoReturnNoPara item in method.GetInvocationList())
                {
                    item.Invoke();
                }

                // -= 为委托实例移除方法, 从方法链的尾部开始匹配,
                // 遇到第一个完全吻合的,移除且移除一个,没有也不异常
                method -= new NoReturnNoPara(this.DoNothing);
                method -= new NoReturnNoPara(DoNothingStatic);
                method -= new NoReturnNoPara(Student.StudyAdvanced);
                method -= new NoReturnNoPara(new Student().Study);
                method -= new NoReturnNoPara(student.Study);

                method.Invoke();

                {
                    WithReturnNoPara method1 = new WithReturnNoPara(this.GetSomething);

                    method1 += new WithReturnNoPara(this.GetSomething2);
                    method1 += new WithReturnNoPara(this.GetSomething3);

                    int result = method1.Invoke(); //多播委托带返回值,结果以最后的为准
                    Console.WriteLine(result);
                }
            }
        }