示例#1
0
        static void Main(string[] args)
        {
            //4.定义委托
            MyDel delVar;

            //5.关联方法
            MyInstObj myInstObj = new MyInstObj();

            //第一种绑定的样式
            //delVar = new MyDel(myInstObj.MyM1);//关联实例方法
            //dVar = new MyDel(SClass.OtherM2);//关联静态的方法
            //第二种绑定的样式 - 缩写形式
            delVar  = myInstObj.MyM1; //关联实例方法
            delVar += SClass.OtherM2; //关联静态的方法

            //+= / -= 添加或删除委托

            //组合委托 +
            MyDel delA = myInstObj.MyM1;
            MyDel delB = SClass.OtherM2;

            MyDel delC = delA + delB;

            //6.调用方法
            //delVar(1);
            delC(1);

            //等待
            Console.ReadKey();
        }
示例#2
0
        public static string Convert(string s)
        {
            MyDel md = (string c) =>
            {
                if (engLett.Where(x => x.lett == c) == null)
                {
                    return(c);
                }
                var str = (from x in engLett
                           where x.lett == c
                           select x into x1
                           from y in armLett
                           where x1.id == y.id
                           select y);



                return(str.ElementAt(0).lett);
            };
            string myst = " ";

            for (int i = 0; i < s.Length; i++)
            {
                myst += md("" + s[i]);
            }
            return(myst.ToString());
        }
示例#3
0
        static void Main()
        {
            Console.WriteLine("Введите число элементов массива:");
            int n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(new string('-', 50));

            var array = new MyDelegate[n];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = () => new MyDelegate(GetRandom).Invoke();
            }

            MyDel d = delegate(MyDelegate[] c)
            {
                double sr = 0;
                for (int i = 0; i < c.Length; i++)
                {
                    sr += c[i].Invoke();
                }
                return(sr / array.Length);
            };

            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i].Invoke() + " ");
            }

            Console.WriteLine("\nСреднее арифметическое элементов {0:##.###}", d(array));

            //Delay
            Console.ReadKey();
        }
示例#4
0
        private static void Main(string[] args)
        {
            shuixiang shuixiang1 = new shuixiang(10);
            jiashuiqi jiashuiqi1 = new jiashuiqi();
            jingbaoqi jingbaoqi1 = new jingbaoqi();

            shuixiang1.shuixiangkong += jiashuiqi1.jiashui;
            shuixiang1.shuixiangkong += jingbaoqi1.baojing;
            //shuixiang1.tiji = 10;
            MyDel myDel  = new MyDel(SayHello);
            MyDel myDel1 = SayHello;
            MyDel chain  = null;

            chain += myDel;
            chain += myDel1;
            //myDel("hello");
            //myDel1("hello11");
            chain("a");
            while (true)
            {
                Thread.Sleep(1000);
                shuixiang1.fangshui();
                if (shuixiang1.tiji <= 0)
                {
                    //shuixiang1.jiashui();
                    shuixiang1.shuixiangkongle();
                    //break;
                }
            }
        }
示例#5
0
        static void Main()
        {
            var array = new MyDelegate[50];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = () => new MyDelegate(GetRandom).Invoke();
            }

            MyDel d = delegate(MyDelegate[] c)
            {
                double sr = 0;
                for (int i = 0; i < c.Length; i++)
                {
                    sr += c[i].Invoke();
                }
                return(sr / array.Length);
            };

            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i].Invoke() + " ");
            }

            Console.WriteLine("\nСреднее арифметическое элементов {0:##.###}", d(array));


            Console.ReadKey();
        }
示例#6
0
        static void Main(string[] args)
        {
            //создание массыва делегатов
            MyDel[] delArray = new MyDel[3];

            //Random
            Random rnd = new Random();

            //методы сообщенные с делегатом MyDel для массива
            delArray[0] = delegate { return(rnd.Next(1, 100)); };
            delArray[1] = delegate { return(rnd.Next(1, 100)); };
            delArray[2] = delegate { return(rnd.Next(1, 100)); };


            //метод сообщенный с делегатом MyDelegate
            MyDelegate myDelegate = delegate(MyDel[] array)
            {
                int count    = 0;
                int maxCount = array.Length;
                for (int i = 0; i < maxCount; i++)
                {
                    count += array[i].Invoke();
                    Console.WriteLine(count);
                }
                return(count / maxCount);
            };


            Console.WriteLine("MyDelegate = " + myDelegate(delArray));

            Console.ReadKey();
        }
示例#7
0
 public Form2()
 {
     InitializeComponent();
     Sender = new MyDel(getMSSV);
     setCBB_Class();
     rdFemale.Checked = true;
 }
示例#8
0
        public BasicPlayer(string name, gender gender, int age = _initValue) : this(name, gender, _initValue,
                                                                                    _initValue, age)
        {
            MyDel del = () => { Console.WriteLine("角色生成!"); };

            del();
        }
示例#9
0
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();

            MyDel md = mc.run;

            //IAsyncResult iar = md.BeginInvoke(null, null);

            //md.EndInvoke(iar);
            //Thread t = new Thread(mc.run);
            //t.Start();

            Action ac = mc.run;

            ac += mc.run2;
            ac += mc.run2;

            foreach (var s in ac.GetInvocationList())
            {
                Console.WriteLine(s.Method);
            }

            //Delegate.Remove(ac, mc.run2);
            ac -= mc.run2;

            foreach (var s in ac.GetInvocationList())
            {
                Console.WriteLine(s.Method);
            }
        }
        public static int MainMethod(string[] ars)
        {
            Test    t  = new Test();
            dynamic d  = t;
            dynamic c  = (char)2;
            dynamic c0 = c;

            d.field *= c0;
            c        = 5;
            d.field /= Method(c);
            sbyte s = 3;

            c        = s;
            d.field %= t[c];
            if (d.field != 1)
            {
                return(1);
            }
            MyDel   md  = Method;
            dynamic dmd = new MyDel(Method);

            c        = 2;
            d.field += md(c);
            if (d.field != 3)
            {
                return(1);
            }
            d.field -= dmd(2);
            if (d.field != 1)
            {
                return(1);
            }
            return((int)(d.field -= d.LongPro));
        }
示例#11
0
        static void Main(string[] args)
        {
            MyDel m1 = (word1, word2) => word1 + word2;

            Console.WriteLine(m1("Hello ", "World!"));
            Console.ReadKey();
        }
示例#12
0
 public Form2()
 {
     InitializeComponent();
     Option = new MyDel(setDelegate);
     radioButton1.Checked = true;
     setCBB();
 }
示例#13
0
 // Use this for initialization
 void Start()
 {
     charList = new List <oldCharacter>();
     Debug.Log("Binding delegate in Party.cs...");
     myDel = GetComponentInParent <Mastermind>().checkActive;
     Debug.Log("Delegate bound!");
 }
示例#14
0
    private void MyForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        MyDel  del          = ShowMyDialog;
        MyForm mySecondForm = new MyForm();

        this.Owner.BeginInvoke(del, mySecondForm, this.Owner);
    }
示例#15
0
        static void Main(string[] args)
        {
            CountIt count = () => Console.WriteLine("I am in Lambda Expression");

            count();

            MyDel d1 = (int x) => x + x;

            int y = d1(4);

            Console.WriteLine("y = " + y);

            d1 = z => z * z;

            int k = d1(3);

            Console.WriteLine(" k = " + k);

            MyDel2 d2 = (x, h) =>
            {
                Console.WriteLine("x = " + x + " h= " + h);
                return(x + h);
            };

            int g = d2(1, 2);

            Console.WriteLine("g =" + g);
        }
示例#16
0
        static void SomeCallBack(IAsyncResult result)
        {
            MyDel delObj  = (MyDel)result.AsyncState;
            int   result1 = delObj.EndInvoke(result);

            Console.WriteLine("result is {0}", result1);
        }
        private void ExecuteInvoke()
        {
            MyDel del = new MyDel(ExecuteBlockSingle);

            IAsyncResult[] iarArray = new IAsyncResult[100];

            for (int i = 0; i < 100; i++)
            {
                iarArray[i] = del.BeginInvoke(Convert.ToString(i), null, null);
            }

            Console.WriteLine("Doing Stuff");

            bool[] re = new bool[100];
            for (int i = 0; i < 100; i++)
            {
                re[i] = del.EndInvoke(iarArray[i]);
            }

            for (int i = 0; i < 100; i++)
            {
                if (i % 10 == 0)
                {
                    Console.WriteLine();
                }
                Console.Write("{0}\t", re[i]);
            }

            Console.WriteLine("Done");
        }
示例#18
0
        /// <summary>
        /// Shows different ways to set or execute a delegate
        /// </summary>
        public void Test()
        {
            MyDel x1;

            // Standard (Instanz-Methode) C# 1.0 / 2.0

            x1 = new MyDel(this.Print);
            x1 = this.Print;

            // Standard (Statische Meth.) C# 1.0 / 2.0
            x1 = new MyDel(Examples.PrintStatic);
            x1 = Examples.PrintStatic;

            // Anonymous Delegate
            x1 = delegate(string sender) { Console.WriteLine(sender); };

            // Anonymous Delegate (Kurzform)
            x1 = delegate { Console.WriteLine("Hello"); };

            // Lambda Expression (LINQ / später)
            x1 = sender => Console.WriteLine(sender);

            // Statement Lambda Expr. (LINQ / später)
            x1 = sender => { Console.WriteLine(sender); };
        }
 public Form2()
 {
     d = new MyDel(getIDSP);
     InitializeComponent();
     SetCBBMH();
     rbtn_Con.Checked = true;
 }
示例#20
0
 public State(MyDel del, Show form_show, string username)
 {
     this.mydel     = del;
     this.form_show = form_show;
     this.username  = username;
     InitializeComponent();
 }
示例#21
0
 public Form2()
 {
     InitializeComponent();
     Sender = new MyDel(getID);
     setCbbMH();
     rbtn_Con.Checked = true;
 }
        public static int MainMethod(string[] ars)
        {
            Test    t  = new Test();
            dynamic d  = t;
            char    c  = (char)2;
            dynamic c0 = c;

            d[null] *= c0;
            dynamic i = 5;

            d[1] /= Method(i);
            sbyte   s  = 3;
            dynamic s0 = s;

            d[default(long)] %= t[s0, ""];
            if (d[default(string)] != 1)
            {
                return(1);
            }
            MyDel   md  = Method;
            dynamic dmd = new MyDel(Method);
            dynamic md0 = 2;

            d[default(dynamic)] += md(md0);
            if (d[12.34f] != 3)
            {
                return(1);
            }
            d[typeof(Test)] -= dmd(2);
            if (d[""] != 1)
            {
                return(1);
            }
            return((int)(d[new Test()] -= LongPro));
        }
示例#23
0
文件: Form1.cs 项目: cmwldysz/vision
        delegate void MyDel(int value);//声明委托

        private void button1_Click(object sender, EventArgs e)
        {
            void printLow(int value)
            {
                Console.WriteLine("{0}-Low Volue", value);
            }

            void PrintHight(int value)
            {
                Console.WriteLine("{0}-Hight Value", value);
            }

            void PrintNumber(double value)
            {
                Console.WriteLine("{0}-Value", value);
            }

            //创建一个随机数
            Random rand      = new Random();
            int    randValue = rand.Next(99);
            //声明一个委托
            MyDel del;

            //使用委托
            del = randValue < 50 ? new MyDel(printLow) : new MyDel(PrintHight);
            //执行委托
            del(randValue);
            MyDel del2 = printLow;
        }
        public static int MainMethod(string[] ars)
        {
            Test    t = new Test();
            dynamic d = t;
            char    c = (char)2;

            d.field *= c;
            int i = 5;

            d.field /= Method(i);
            sbyte s = 3;

            d.field %= t[s];
            if (d.field != 1)
            {
                return(1);
            }
            MyDel   md  = Method;
            dynamic dmd = new MyDel(Method);

            d.field += md(2);
            if (d.field != 3)
            {
                return(1);
            }
            d.field -= dmd(2);
            if (d.field != 1)
            {
                return(1);
            }
            return((int)(d.field -= LongPro));
        }
示例#25
0
        public DelCallbackTest()
        {
            //create delegate class
            DelDriver driver = new DelDriver();
            MyDel     d1     = showInfo;

            driver.delDriver(d1);
        }
示例#26
0
 private void ThreadMethod()
 {
     //耗时操作
     Thread.Sleep(10000);
     //传给窗体,不耗时操作
     this.btnInvoke.Invoke(new MyDel(DelMethod));
     MyDel md = null;
 }
示例#27
0
        public CH0804()
        {
            var mydel = new MyDel <string>(DelClass.PrintStr);

            mydel += DelClass.PrintUpperStr;

            mydel("Hello There");
        }
示例#28
0
文件: MyDel.cs 项目: shivaramani/.net
    static void Main()
    {
        // Instantiate delegate with named method
        MyDel d1 = MyDelMethod;

        // Invoke the delegate
        d1("Hello");
    }
示例#29
0
 public static void DoSth(MyDel mdl)
 {
     Console.WriteLine("吃早饭");
     Console.WriteLine("看书");
     Console.WriteLine("XXXXXX");
     Console.WriteLine("YYYYY");
     //Console.WriteLine("做什么事儿不确定");
     mdl();
 }
示例#30
0
        static void Main(string[] args)
        {
            MyDel md = MyMethod;

            Console.WriteLine(md);

            md("라라라");
            md("루루루");
        }
示例#31
0
        public void TestMethod1()
        {
            MyDel del = new MyDel(MyMethod);
            del += MyMethod2;
            del += MyMethod3;

            string res = del("NewS");

            Assert.AreEqual("News", res);
        }
示例#32
0
        static void Main(string[] args)
        {
			MyDel del = new MyDel(Sum);

			Console.WriteLine("Before BeginInvoke");
			IAsyncResult iar = del.BeginInvoke(10, 10, new AsyncCallback(CallWhenDone), null);
			
			Console.WriteLine("Doing more work in Main");
			Thread.Sleep(3000);
			Console.WriteLine("Done with Main. Exiting.");
		}
示例#33
0
        static void Main(string[] args)
        {
			MyDel del = new MyDel(Sum);

			Console.WriteLine("Before BeginInvoke");
			IAsyncResult iar = del.BeginInvoke(8, 9, null, null);	// Start async
			Console.WriteLine("After BeginInvoke");

			Console.WriteLine("Doing Stuff...");
			
			long result = del.EndInvoke(iar);	// Wait for end and get result.
			Console.WriteLine("After EndInvoke {0}", result);
		}
示例#34
0
        static void Main()
        {
            MyDel del = new MyDel(Sum);
            Console.WriteLine("Before BeginInvoke");
            IAsyncResult iar = del.BeginInvoke(3, 5, new AsyncCallback(CallWhenDone), "Hello world!");
            // IAsyncResult iar = del.BeginInvoke(3, 5, CallWhenDone, null);

            Console.WriteLine("Doing more work in Main.");
            Thread.Sleep(500);
            Console.WriteLine("Done with Main. Exiting.");

            Console.ReadLine();
        }
示例#35
0
        static void Main()
        {
            //AsyncResult represents the state of the asynchronous method
            //IAsyncResult  IsCompleted  AsyncState
            //AsyncResult   AsyncDelegate

            MyDel del = new MyDel(Sum);

            Console.WriteLine("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);

            Console.WriteLine("Before BeginInvoke");
            IAsyncResult iar = del.BeginInvoke(3, 5, null, null); // Start async
            Console.WriteLine("After BeginInvoke");
            Console.WriteLine("Doing stuff");
            long result = del.EndInvoke(iar); // Wait for end and get result
            Console.WriteLine("After EndInvoke: {0}", result);

            Console.ReadLine();
        }
示例#36
0
        static void Main(string[] args)
        {
			MyDel del = new MyDel(Sum);	

			IAsyncResult iar = del.BeginInvoke(11, 12, null, null);
			Console.WriteLine("After BeginInvoke");
			
			// Check whether the asynchronous method is done.
			while (!iar.IsCompleted)
			{
				Console.WriteLine(" Not Done");
				for (int i = 0; i < 10000000; i++)
				{
					// Continue processing, even though in this case it's just busywork.
				}
			}
			Console.WriteLine("Done");
			long result = del.EndInvoke(iar); // Call EndInvoke to get result and clean up.
			Console.WriteLine("Result: {0}", result);
		}
示例#37
0
        static void Main()
        {
            MyDel del = new MyDel(Sum);

            Console.WriteLine("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);

            IAsyncResult iar =                    // Start async.
               del.BeginInvoke(3, 5, null, null); // Spawn async method.
            Console.WriteLine("After BeginInvoke");

            /*
             * You can use the "IsCompleted" method if you are polling for example,
             * you could check if the method has finished, and if so, you could invoke EndInvoke.
             *
             * You can also use the WaitHandle that is returned by the AsyncWaitHandle to monitor async method.
             * You can use the WaitOne method, WaitAll or WaitAny to get finer control,
             * they are all essentially the same operation, but it applies to the three potential conditions:
             * single wait, waiting for all the handles to complete,
             * or wait for any handles to complete (WaitHandles are not limited to be used with async methods,
             * you can obtain those from other parts of the framework,
             * like network communications, or from your own thread synchronization operations).
             * */

             // while (!iar.AsyncWaitHandle.WaitOne(1000, true))
            //  while (!iar.IsCompleted)

            while (!iar.IsCompleted)            // Check whether async method is done.
            {
                Console.WriteLine("Not Done");
                // Continue processing, even though in this case it's just busywork.
                for (long i = 0; i < 10000000; i++)
                    ;
            }

            Console.WriteLine("Done");
            long result = del.EndInvoke(iar);   // Call EndInvoke to get result and clean up.
            Console.WriteLine("Result: {0}", result);

            Console.ReadLine();
        }
 public static int MainMethod(string[] ars)
 {
     Test t = new Test();
     dynamic d = t;
     char c = (char)2;
     d.field *= c;
     int i = 5;
     d.field /= Method(i);
     sbyte s = 3;
     d.field %= t[s];
     if (d.field != 1)
         return 1;
     MyDel md = Method;
     dynamic dmd = new MyDel(Method);
     d.field += md(2);
     if (d.field != 3)
         return 1;
     d.field -= dmd(2);
     if (d.field != 1)
         return 1;
     return (int)(d.field -= LongPro);
 }
示例#39
0
        static void Main(string[] args)
        {
            // MyDel example
              MyDel delInt = new MyDel(SecondPowerOf);
              int i = delInt(2);
              Console.WriteLine(i);

              delInt = ThirdPowerOf;   // shorter syntax
              i = delInt(2);
              Console.WriteLine(i);

              Console.WriteLine();

              // PrintDelegate example
              PrintDelegate print = FirstPrint;
              print += SecondPrint;
              print += ThirdPrint;

              print("string literal");

              Console.ReadLine();  // wait for <ENTER>
        }
 public static int MainMethod(string[] ars)
 {
     Test t = new Test();
     dynamic d = t;
     dynamic c = (char)2;
     dynamic c0 = c;
     d.field *= c0;
     c = 5;
     d.field /= Method(c);
     sbyte s = 3;
     c = s;
     d.field %= t[c];
     if (d.field != 1)
         return 1;
     MyDel md = Method;
     dynamic dmd = new MyDel(Method);
     c = 2;
     d.field += md(c);
     if (d.field != 3)
         return 1;
     d.field -= dmd(2);
     if (d.field != 1)
         return 1;
     return (int)(d.field -= d.LongPro);
 }
 public static int MainMethod(string[] ars)
 {
     Test t = new Test();
     dynamic d = t;
     char c = (char)2;
     dynamic c0 = c;
     d[null] *= c0;
     dynamic i = 5;
     d[1] /= Method(i);
     sbyte s = 3;
     dynamic s0 = s;
     d[default(long)] %= t[s0, ""];
     if (d[default(string)] != 1)
         return 1;
     MyDel md = Method;
     dynamic dmd = new MyDel(Method);
     dynamic md0 = 2;
     d[default(dynamic)] += md(md0);
     if (d[12.34f] != 3)
         return 1;
     d[typeof(Test)] -= dmd(2);
     if (d[""] != 1)
         return 1;
     return (int)(d[new Test()] -= LongPro);
 }
 public static int MainMethod(string[] ars)
 {
     Test t = new Test();
     MyDel md = new MyDel(Method);
     t.field *= t.LongPro %= t[(sbyte)10] -= md(3);
     if (t.field != 40 || t.LongPro != 4 || t[null] != 5)
         return 1;
     return 0;
 }
 public static int MainMethod(string[] ars)
 {
     dynamic t = new Test();
     MyDel md = new MyDel(Method);
     t.field *= t.LongPro += t[(int)10] -= md(3);
     if (t.field != 50 || t.LongPro != 5 || t[null] != 5)
         return 1;
     return 0;
 }
        public static int MainMethod(string[] ars)
        {
            Test t = new Test();
            dynamic d = t;
            try
            {
                d[10] += new MyDel(delegate (int x)
                {
                    return x;
                }

                );
            }
            catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
            {
                if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "+=", "int", "Test.MyDel"))
                    return 0;
            }

            return 1;
        }
示例#45
0
 public static void CallBackTest()
 {
     MyDel mydel = new MyDel(Sum);
     mydel.BeginInvoke(3, 4, new AsyncCallback(CallBackWhenDone), (object)"我是参数");
     Console.WriteLine("退出方法");
     return;
 }