예제 #1
0
        static void Main(string[] args)
        {
            // 创建委托对象1,绑定一个静态方法
            DelegateTest dtstatic = new DelegateTest(methodStatic);
            // 创建委托对象2,绑定一个实例方法
            DelegateTest dtinstance = new DelegateTest(new Captal8_Delegate_04DelegateChain().methodInstance);

            // 创建一个委托对象,并绑定委托对象1,使用"+"添加委托对象2,成为委托链
            DelegateTest delegateChain = dtstatic;

            delegateChain += dtinstance;

            // 调用委托对象
            delegateChain.Invoke();

            Console.WriteLine();
            Console.WriteLine("从委托链上移除静态方法后:");

            // 从委托链上移除静态方法
            delegateChain -= dtstatic;

            delegateChain.Invoke();

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // 用静态方法来实例化委托
            DelegateTest dtstatic = new DelegateTest(Program.method1);

            DelegateTest dtinstance = new DelegateTest(new Program().method2);

            // 定义一个委托对象,一开始初始化为null,就是不代表任何方法(我就是我,我不代表任何人)
            DelegateTest delegatechain = null;

            // 使用+符号链接委托,链接多个委托后就成为委托链了
            delegatechain += dtstatic;
            delegatechain += dtinstance;
            Console.WriteLine("delegatechain's number methods : " + delegatechain.GetInvocationList().Length);
            // 使用-运算符把dtstatic委托从委托链中移除
            delegatechain -= dtstatic;

            // 调用委托链
            delegatechain.Invoke();
            Console.Read();
        }
예제 #3
0
        static void Main(string[] args)
        {
            DelegateTest funcRef = new DelegateTest(Test);

            Test("1");
            funcRef.Invoke("2");

            DelegateTest funcRef2 = Test;

            funcRef2 += Test2;
            funcRef2.Invoke("2");

            BeregnerDelegate beregner = Add;

            beregner += Sub;
            Console.ForegroundColor = ConsoleColor.Yellow;
            foreach (var f in beregner.GetInvocationList())
            {
                Console.WriteLine(f.DynamicInvoke(5, 5));
            }

            Console.WriteLine();
            var fu = FindFunktion();

            Console.WriteLine(fu(5, 5));
            Console.WriteLine(FindFunktion()(4, 4));


            //

            Action <string> minA = Test;

            minA += Test2;
            minA("xfljldjdkjfg");

            Action <string, int, DateTime> minB;

            // sidste args definerer returtypen
            Func <int, int, int> MinBeregner = Add;

            MinBeregner(5, 4);

            Predicate <int> testp = (i) => i < 10;
            var             e     = testp(1);


            Hund h = new Hund();


            System.IO.FileSystemWatcher fw
                = new System.IO.FileSystemWatcher(@"c:\temp");
            //fw.Created += Fw_Created;
            fw.Created += (sender, evt) => {
                Console.WriteLine(evt.FullPath);
            };

            fw.EnableRaisingEvents = true;
            //do
            //{

            //} while (true);

            Console.ForegroundColor = ConsoleColor.Yellow;
            Hund g = new Hund();

            //g.PropertyChanged += G_PropertyChanged;

            //g.PropertyChanged += delegate (object se, MyEventArgs ev) {
            //    Console.WriteLine("Egenskab rettet " + ev.PropName);
            //};

            g.PropertyChanged += (object se, MyEventArgs ev) => {
                Console.WriteLine("Egenskab rettet " + ev.PropName);
            };

            g.Name = "1";
            g.Name = "2";
            g.Name = "3";
            g.Age  = 10;
        }