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 中,它必須由 ... End Sub 建構定義 Sub 。它也可以是傳回忽略值的方法。) 一般而言,這類方法是用來執行作業。

注意

若要參考沒有參數並傳回值的方法,請改用泛型 Func<TResult> 委派。

當您使用委派時 Action ,不需要明確定義封裝無參數程式的委派。 例如,下列程式碼會明確宣告名為 ShowValue 的委派,並將實例方法的 Name.DisplayToWindow 參考指派給其委派實例。

using namespace System;
using namespace System::Windows::Forms;

public delegate void ShowValue();


public ref class Name
{
private:
   String^ instanceName;

public:
   Name(String^ name) 
   {
      instanceName = name;
   }

   void DisplayToConsole()
   {
      Console::WriteLine(this->instanceName);
   }

   void DisplayToWindow()
   {
      MessageBox::Show(this->instanceName);
   }
};

int main()
{
   Name^ testName = gcnew Name(L"Koani");
   ShowValue^ showMethod;
   showMethod = gcnew ShowValue(testName, &Name::DisplayToWindow);
   showMethod();
   return 0;
}
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.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

public ref class Name
{
private:
   String^ instanceName;
   
public:
   Name(String^ name)
   {
      instanceName = name;
   }

   void DisplayToConsole()
   {
      Console::WriteLine(this->instanceName);
   }

   void DisplayToWindow()
   {
      MessageBox::Show(this->instanceName);
   }
};


int main()
{
   Name^ testName = gcnew Name(L"Koani");
   System::Action^ showMethod;
   showMethod += gcnew Action(testName, &Name::DisplayToWindow);
   showMethod();
   return 0;
}
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

您也可以在 Action C# 中使用委派搭配匿名方法,如下列範例所示。 (如需匿名方法的簡介,請參閱 Anonymous Methods.)

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

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱