我所知道的委託

來源:互聯網
上載者:User
一、什麼是委託1.1官方解釋

委託是一種定義方法簽名的類型。當執行個體化委託時,您可以將其執行個體與任何具有相容簽名的方法相關聯。您可以通過委託執行個體調用方法。

1.2個人理解

委託就是執行方法(函數)的一個類。

事件是一種特殊的委託。

二、如何申明委託2.1 delegate

        public delegate int TestDelegate(int x, int y);

2.2 Action

       Action是無傳回值的泛型委派。

Action 表示無參,無傳回值的委託

Action<int,string> 表示有傳入參數int,string無傳回值的委託

2.3 Func

Func是有傳回值的泛型委派

Func<int> 表示無參,傳回值為int的委託

Func<object,string,int> 表示傳入參數為object, string 傳回值為int的委託

2.4 predicate

predicate 是返回bool型的泛型委派

predicate<int> 表示傳入參數為int 返回bool的委託。

2.5 四者之間的區別

Delegate至少0個參數,至多32個參數,可以無傳回值,也可以指定傳回值類型

Action至少1個參數,至多4個參數,無傳回值,

Func至少0個參數,至多4個參數,根據傳回值泛型返回。必須有傳回值,不可void

Predicate至少1個參數,至多1個參數,傳回值固定為bool

三、如何使用委託3.1 Labmda運算式

TestDelegate d2= (string name) => { Console.WriteLine("你好,{0}!", name); };

d2("Terry");

3.2匿名方法

delegate void TestDelegate(string myName);

TestDelegate d2 = delegate(string name)
{

Console.WriteLine("Hello,{0}!", name);

};

d2(“Test”);

3.3 函數申明

private void DelegateMethod(string name)

{

Console.WriteLine("Hello,{0}!", name);

       }

       TestDelegate d2 = new TestDelegate(DelegateMethod);

       d2(“Test”);

四、使用委託有哪些特點

委託類似於 C++ 函數指標,但它們是型別安全的。

委託允許將方法作為參數進行傳遞。

委託可用於定義回調方法。

委託可以連結在一起;例如,可以對一個事件調用多個方法。

方法不必與委託簽名完全符合。

五、委託使用情境

委託一般都使用在 Observer模式(觀察者模式)。

Observer設計模式是為了定義對象間的一種一對多的依賴關係,以便於當一個對象的狀態改變時,其他依賴於它的對象會被自動告知並更新。

Observer模式主要包括如下兩類對象:

被監視對象:往往包含著其他對象所感興趣的內容。

監視者:當對象中的某件事發生的時候,會告知建設者,而建設者則會採取相應的行動。

例如:當你程式處理大批量資料時,需要在程式介面顯示進度條進行友好提示,這時你通過委託來實現相當方便。

範例:

public delegate void DelegateMethod(int position, int maxValue);

  public class TestDelegate
    {
        public DelegateMethod OnDelegate;

        public void DoDelegateMethod()
        {
            int maxValue = 100;
            for (int i = 0; i < maxValue; i++)
            {
                if (this.OnDelegate != null)
                {
                    this.OnDelegate(i, maxValue);
                }
            }
        }

}

TestDelegate test = new TestDelegate();
            this.textBox1.Text = "";
            this.progressBar1.Value = 0;
            test.OnDelegate = new DelegateMethod(delegate(int i, int maxValue)
            {
                this.textBox1.Text += i.ToString() + Environment.NewLine;
                this.progressBar1.Maximum = maxValue;
                this.progressBar1.Value++;
            });
            test.DoDelegateMethod();
六、如何清空委託

1、在類中申明清空委託方法,依次迴圈去除委託引用。

         方法如下:

public class TestDelegate
    {
        public DelegateMethod OnDelegate;

                 public void ClearDelegate()
        {
            while (this.OnDelegate != null)
            {
                this.OnDelegate -= this.OnDelegate;
            }
        }
    }
2、如果在類中沒有申明清空委託的方法,我們可以利用GetInvocationList查詢出委託引用,然後進行去除。

方法如下:

TestDelegate test = new TestDelegate();

if (test.OnDelegate != null)
{
  System.Delegate[] dels = test.OnDelegate.GetInvocationList();
  for (int i = 0; i < dels.Length; i++)
  {
     test.OnDelegate -= dels[i] as DelegateMethod;
  }
}

 

插入

 

我在實際中用到的委託

聲明:

 private delegate void fnShowItem();
  private Dictionary<String, fnShowItem> callShowItem;

 

執行個體:

callShowItem.Add("補充新員工資料", ShowItemForNewEmployee);
            callShowItem.Add("批量更改班次", ShowItemForShift);
            callShowItem.Add("批量更改主管", ShowItemForSuper);
            callShowItem.Add("批量更改資深主管", ShowItemForSrSuper);
            callShowItem.Add("批量更改組長", ShowItemForLeader);

 

執行個體:

  private void ShowItemForNewEmployee()
        {
            HideAll();

            this.cbBelongLocation.Visible = true;
            this.comboLine.Visible = true;
            // this.comboProcess.Visible = true;
            //  this.comboQuarters.Visible = true;
            //  this.comboCraftwork.Visible = true;
            this.txtBelongLeader.Visible = true;
            this.txtBelongSuper.Visible = true;

            //this.btn_SLeader.Visible = true;
            //this.btn_SSuper.Visible = true;

            //  this.txtQuartersKind.Visible = true;

            this.cbWIPInfo.Visible = true;

            this.btn_NewAdd.Visible = true;

            SetTipText("Default", Color.Maroon);
            SetSrSupervisor(" Where Mark = 'A' ");
        }

 

調用:

this.callShowItem["批量更改班次"]();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.