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

还可以在 C# 中将 Action 委托与匿名方法一起使用,如以下示例所示。 (有关匿名方法的简介,请参阅 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)

获取指示指定委托表示的方法的对象。

适用于

另请参阅