英語で読む

次の方法で共有


MethodInvoker 代理人

定義

void として宣言された、パラメーターをとらない任意のメソッドを、マネージド コード内で実行できるデリゲートを表します。

public delegate void MethodInvoker();

次のコード例では、 を使用 MethodInvoker して、アプリケーション フォームのタイトル バーを更新するメソッドを呼び出す方法を示します。

public partial class Form1 : Form
{
    public Form1()
    {
        // Create a timer that will call the ShowTime method every second.
        var timer = new System.Threading.Timer(ShowTime, null, 0, 1000);           
    }

    private void ShowTime(object x)
    {
        // Don't do anything if the form's handle hasn't been created 
        // or the form has been disposed.
        if (!this.IsHandleCreated || this.IsDisposed) return;
        
        // Invoke an anonymous method on the thread of the form.
        this.Invoke((MethodInvoker) delegate
        {
            // Show the current time in the form's title bar.
            this.Text = DateTime.Now.ToLongTimeString();
        });
    }
}

注釈

MethodInvoker は、void パラメーター リストを使用してメソッドを呼び出すために使用される単純なデリゲートを提供します。 このデリゲートは、コントロールの Invoke メソッドを呼び出す場合、または単純なデリゲートが必要だが、自分で定義したくない場合に使用できます。

拡張メソッド

GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10