摘要:(200-07-23) LINQ Delegate(委派)
委派 ( Delegate 定義Method 結構 )
與 Method 有依存關係
委派沒有Type 不屬於事前規劃類別部份,屬於應用程式部份做彈性應用
//定義委派
namespace util
{
public delegate void hello(string who);
}實作委派
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace linqwinpart2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//委派咬過來的程序
private void engHello(String who)
{
MessageBox.Show(who + " Hello World!!");
}
private void button1_Click(object sender, EventArgs e)
{
//同步Method call(執行階段)
this.engHello("eric");
//代理者delegate(找一個可以結合 method咬住)
//不是立即要呼叫
util.hello hanlder = new util.hello(engHello);
//....
//呼換
hanlder.Invoke("linda");
}
}
}
傳遞 Delegate Method
private void button2_Click(object sender, EventArgs e)
{
util.hello handler = new util.hello(engHello);
//建立泛型委派
util.hello<String> handler2 = new util.hello<string>(engHello);
//呼叫處理委派Method
invokeDelegate(handler);
}
//傳遞delegate Method(proxy Method)
private void invokeDelegate(util.hello handler)
{
handler("Linda");
}