Action 代理人

定義

封裝了沒有參數且不回傳值的方法。

public delegate void Action();
public delegate void Action();
type Action = delegate of unit -> unit
Public Delegate Sub Action()

備註

你可以用這個代理程式將方法當作參數傳遞,而不必明確宣告自訂代理。 封裝方法必須對應於此代理所定義的方法簽名。 這表示封裝方法必須沒有參數,也沒有返回值。 (在 C# 中,方法必須回傳 void。在 F# 中,函式或方法必須回傳 unit。在 Visual Basic 中,必須由 Sub...End Sub 結構來定義。它也可以是回傳一個被忽略的值。)通常,這類方法用於執行操作。

備註

若要參考沒有參數且回傳值的方法,請使用通用 Func<TResult> 代理。

使用 Action 代理時,不必明確定義一個封裝無參數程序的代理。 例如,以下程式碼明確宣告一個名為代理 ShowValue 的代理,並將該代理方法的參考 Name.DisplayToWindow 指派給其代理實例。

using System;
using System.Windows.Forms;

public delegate void ShowValue();

public class Name
{
   private string instanceName;

   public Name(string name)
   {
      this.instanceName = name;
   }

   public void DisplayToConsole()
   {
      Console.WriteLine(this.instanceName);
   }

   public void DisplayToWindow()
   {
      MessageBox.Show(this.instanceName);
   }
}

public class testTestDelegate
{
   public static void Main()
   {
      Name testName = new Name("Koani");
      ShowValue showMethod = testName.DisplayToWindow;
      showMethod();
   }
}
open System.Windows.Forms

type ShowValue = delegate of unit -> unit

type Name(name) =
    member _.DisplayToConsole() = 
        printfn "%s" name

    member _.DisplayToWindow() = 
        MessageBox.Show name |> ignore

let testName = Name "Koani"

let showMethod = ShowValue testName.DisplayToWindow

showMethod.Invoke()
Public Delegate Sub ShowValue

Public Class Name
   Private instanceName As String
   
   Public Sub New(name As String)
      Me.instanceName = name
   End Sub
   
   Public Sub DisplayToConsole()
      Console.WriteLine(Me.instanceName)
   End Sub   
   
   Public Sub DisplayToWindow()
      MsgBox(Me.instanceName)
   End Sub   
End Class

Public Module testDelegate
   Public Sub Main()
      Dim testName As New Name("Koani")
      Dim showMethod As ShowValue = AddressOf testName.DisplayToWindow
      showMethod   
   End Sub
End Module

以下範例簡化了此程式碼,透過實 Action 例化代理,而非明確定義新代理並指派命名方法。

using System;
using System.Windows.Forms;

public class Name
{
   private string instanceName;

   public Name(string name)
   {
      this.instanceName = name;
   }

   public void DisplayToConsole()
   {
      Console.WriteLine(this.instanceName);
   }

   public void DisplayToWindow()
   {
      MessageBox.Show(this.instanceName);
   }
}

public class testTestDelegate
{
   public static void Main()
   {
      Name testName = new Name("Koani");
      Action showMethod = testName.DisplayToWindow;
      showMethod();
   }
}
open System
open System.Windows.Forms

type Name(name) =
    member _.DisplayToConsole() =
        printfn "%s" name

    member _.DisplayToWindow() =
        MessageBox.Show name |> ignore

let testName = Name "Koani"

// unit -> unit functions and methods can be cast to Action.
let showMethod = Action testName.DisplayToWindow

showMethod.Invoke()
Public Class Name
   Private instanceName As String
   
   Public Sub New(name As String)
      Me.instanceName = name
   End Sub
   
   Public Sub DisplayToConsole()
      Console.WriteLine(Me.instanceName)
   End Sub   
   
   Public Sub DisplayToWindow()
      MsgBox(Me.instanceName)
   End Sub   
End Class

Public Module testDelegate
   Public Sub Main()
      Dim testName As New Name("Koani")
      Dim showMethod As Action = AddressOf testName.DisplayToWindow
      showMethod   
   End Sub
End Module

你也可以在 C# 中用匿名方法使用 Action 代理,如下範例所示。 (關於匿名方法的入門,請參見 匿名方法。)

using System;
using System.Windows.Forms;

public class Name
{
   private string instanceName;

   public Name(string name)
   {
      this.instanceName = name;
   }

   public void DisplayToConsole()
   {
      Console.WriteLine(this.instanceName);
   }

   public void DisplayToWindow()
   {
      MessageBox.Show(this.instanceName);
   }
}

public class Anonymous
{
   public static void Main()
   {
      Name testName = new Name("Koani");
      Action showMethod = delegate() { testName.DisplayToWindow();} ;
      showMethod();
   }
}

你也可以將 lambda 運算式指派給 Action 代理實例,如下範例所示。 (關於 lambda 表達式的入門,請參見 Lambda 表達式(C#)Lambda 表達式(F#)。)

using System;
using System.Windows.Forms;

public class Name
{
   private string instanceName;

   public Name(string name)
   {
      this.instanceName = name;
   }

   public void DisplayToConsole()
   {
      Console.WriteLine(this.instanceName);
   }

   public void DisplayToWindow()
   {
      MessageBox.Show(this.instanceName);
   }
}

public class LambdaExpression
{
   public static void Main()
   {
      Name testName = new Name("Koani");
      Action showMethod = () => testName.DisplayToWindow();
      showMethod();
   }
}
open System
open System.Windows.Forms

type Name(name) =
    member _.DisplayToConsole() = 
        printfn "%s" name

    member _.DisplayToWindow() = 
        MessageBox.Show name |> ignore

let testName = Name "Koani"

let showMethod = Action(fun () -> testName.DisplayToWindow())

showMethod.Invoke()
Public Class Name
   Private instanceName As String
   
   Public Sub New(name As String)
      Me.instanceName = name
   End Sub
   
   Public Function DisplayToConsole() As Integer
      Console.WriteLine(Me.instanceName)
      Return 0
   End Function
   
   Public Function DisplayToWindow() As Integer
      Return MsgBox(Me.instanceName)
   End Function      
End Class

Module LambdaExpression
   Public Sub Main()
      Dim name1 As New Name("Koani")
      Dim methodCall As Action = Sub() name1.DisplayToWindow()
      methodCall()
   End Sub
End Module

擴充方法

名稱 Description
GetMethodInfo(Delegate)

取得一個代表指定代理所代表方法的物件。

適用於

另請參閱