Delegate クラス

定義

デリゲートを表します。これは、静的メソッドまたはクラス インスタンスとそのクラスのインスタンス メソッドを参照するデータ構造です。

public ref class Delegate abstract
public ref class Delegate abstract : ICloneable, System::Runtime::Serialization::ISerializable
public abstract class Delegate
public abstract class Delegate : ICloneable, System.Runtime.Serialization.ISerializable
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Serializable]
public abstract class Delegate : ICloneable, System.Runtime.Serialization.ISerializable
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Delegate : ICloneable, System.Runtime.Serialization.ISerializable
type Delegate = class
type Delegate = class
    interface ICloneable
    interface ISerializable
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Serializable>]
type Delegate = class
    interface ICloneable
    interface ISerializable
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Delegate = class
    interface ICloneable
    interface ISerializable
Public MustInherit Class Delegate
Public MustInherit Class Delegate
Implements ICloneable, ISerializable
継承
Delegate
派生
属性
実装

このセクションには、2 つのコード例が含まれています。 最初の例では、このメソッド オーバーロードを使用して作成できる 2 種類のデリゲートを示します。インスタンス メソッドを使用して開き、静的メソッドで開きます。

2 番目のコード例では、互換性のあるパラメーター型と戻り値の型を示します。

例 1

次のコード例は、 CreateDelegate メソッドのこのオーバーロードを使用してデリゲートを作成する 2 つの方法を示しています。

Note

CreateDelegateメソッドには、MethodInfoを指定するオーバーロードが 2 つありますが、最初の引数は指定しません。その機能は同じですが、バインドに失敗した場合にスローするかどうかを指定でき、もう 1 つは常にスローします。 このコード例では、両方のオーバーロードを使用します。

この例では、静的メソッド Cとインスタンス メソッド M2を使用してクラス M1を宣言し、2 つのデリゲート型を宣言します。D1Cと文字列のインスタンスを受け取り、D2は文字列を受け取ります。

Exampleという名前の 2 番目のクラスには、デリゲートを作成するコードが含まれています。

  • 開いているインスタンス メソッドを表す D1型のデリゲートが、インスタンス メソッド M1用に作成されます。 デリゲートが呼び出されたときにインスタンスを渡す必要があります。
  • D2型のデリゲート (オープン静的メソッドを表す) が、静的メソッド M2用に作成されます。
using System;
using System.Reflection;

// Declare three delegate types for demonstrating the combinations
// of static versus instance methods and open versus closed
// delegates.
//
public delegate void D1(C c, string s);
public delegate void D2(string s);
public delegate void D3();

// A sample class with an instance method and a static method.
//
public class C
{
    private int id;
    public C(int id) { this.id = id; }

    public void M1(string s) =>
        Console.WriteLine($"Instance method M1 on C:  id = {this.id}, s = {s}");

    public static void M2(string s)
    {
        Console.WriteLine($"Static method M2 on C:  s = {s}");
    }
}

public class Example2
{
    public static void Main()
    {
        C c1 = new C(42);

        // Get a MethodInfo for each method.
        //
        MethodInfo mi1 = typeof(C).GetMethod("M1",
            BindingFlags.Public | BindingFlags.Instance);
        MethodInfo mi2 = typeof(C).GetMethod("M2",
            BindingFlags.Public | BindingFlags.Static);

        D1 d1;
        D2 d2;
        D3 d3;

        Console.WriteLine("\nAn instance method closed over C.");
        // In this case, the delegate and the
        // method must have the same list of argument types; use
        // delegate type D2 with instance method M1.
        //
        Delegate test =
            Delegate.CreateDelegate(typeof(D2), c1, mi1, false);

        // Because false was specified for throwOnBindFailure
        // in the call to CreateDelegate, the variable 'test'
        // contains null if the method fails to bind (for
        // example, if mi1 happened to represent a method of
        // some class other than C).
        //
        if (test != null)
        {
            d2 = (D2)test;

            // The same instance of C is used every time the
            // delegate is invoked.
            d2("Hello, World!");
            d2("Hi, Mom!");
        }

        Console.WriteLine("\nAn open instance method.");
        // In this case, the delegate has one more
        // argument than the instance method; this argument comes
        // at the beginning, and represents the hidden instance
        // argument of the instance method. Use delegate type D1
        // with instance method M1.
        //
        d1 = (D1)Delegate.CreateDelegate(typeof(D1), null, mi1);

        // An instance of C must be passed in each time the
        // delegate is invoked.
        //
        d1(c1, "Hello, World!");
        d1(new C(5280), "Hi, Mom!");

        Console.WriteLine("\nAn open static method.");
        // In this case, the delegate and the method must
        // have the same list of argument types; use delegate type
        // D2 with static method M2.
        //
        d2 = (D2)Delegate.CreateDelegate(typeof(D2), null, mi2);

        // No instances of C are involved, because this is a static
        // method.
        //
        d2("Hello, World!");
        d2("Hi, Mom!");

        Console.WriteLine("\nA static method closed over the first argument (String).");
        // The delegate must omit the first argument of the method.
        // A string is passed as the firstArgument parameter, and
        // the delegate is bound to this string. Use delegate type
        // D3 with static method M2.
        //
        d3 = (D3)Delegate.CreateDelegate(typeof(D3),
            "Hello, World!", mi2);

        // Each time the delegate is invoked, the same string is
        // used.
        d3();
    }
}

/* This code example produces the following output:

An instance method closed over C.
Instance method M1 on C:  id = 42, s = Hello, World!
Instance method M1 on C:  id = 42, s = Hi, Mom!

An open instance method.
Instance method M1 on C:  id = 42, s = Hello, World!
Instance method M1 on C:  id = 5280, s = Hi, Mom!

An open static method.
Static method M2 on C:  s = Hello, World!
Static method M2 on C:  s = Hi, Mom!

A static method closed over the first argument (String).
Static method M2 on C:  s = Hello, World!
 */
open System
open System.Reflection

// A sample class with an instance method and a static method.
type C(id) =
    member _.M1(s) =
        printfn $"Instance method M1 on C:  id = %i{id}, s = %s{s}"

    static member M2(s) =
        printfn $"Static method M2 on C:  s = %s{s}"
    
// Declare three delegate types for demonstrating the combinations
// of static versus instance methods and open versus closed
// delegates.
type D1 = delegate of C * string -> unit
type D2 = delegate of string -> unit
type D3 = delegate of unit -> unit

let c1 = C 42

// Get a MethodInfo for each method.
//
let mi1 = typeof<C>.GetMethod("M1", BindingFlags.Public ||| BindingFlags.Instance)
let mi2 = typeof<C>.GetMethod("M2", BindingFlags.Public ||| BindingFlags.Static)

printfn "\nAn instance method closed over C."

// In this case, the delegate and the
// method must have the same list of argument types use
// delegate type D2 with instance method M1.
let test = Delegate.CreateDelegate(typeof<D2>, c1, mi1, false)

// Because false was specified for throwOnBindFailure
// in the call to CreateDelegate, the variable 'test'
// contains null if the method fails to bind (for
// example, if mi1 happened to represent a method of
// some class other than C).
if test <> null then
    let d2 = test :?> D2

    // The same instance of C is used every time the
    // delegate is invoked.
    d2.Invoke "Hello, World!"
    d2.Invoke "Hi, Mom!"

printfn "\nAn open instance method."

// In this case, the delegate has one more
// argument than the instance method this argument comes
// at the beginning, and represents the hidden instance
// argument of the instance method. Use delegate type D1
// with instance method M1.
let d1 = Delegate.CreateDelegate(typeof<D1>, null, mi1) :?> D1

// An instance of C must be passed in each time the
// delegate is invoked.
d1.Invoke(c1, "Hello, World!")
d1.Invoke(C 5280, "Hi, Mom!")

printfn "\nAn open static method."
// In this case, the delegate and the method must
// have the same list of argument types use delegate type
// D2 with static method M2.
let d2 = Delegate.CreateDelegate(typeof<D2>, null, mi2) :?> D2

// No instances of C are involved, because this is a static
// method.
d2.Invoke "Hello, World!"
d2.Invoke "Hi, Mom!"

printfn "\nA static method closed over the first argument (String)."
// The delegate must omit the first argument of the method.
// A string is passed as the firstArgument parameter, and
// the delegate is bound to this string. Use delegate type
// D3 with static method M2.
let d3 = Delegate.CreateDelegate(typeof<D3>, "Hello, World!", mi2) :?> D3

// Each time the delegate is invoked, the same string is used.
d3.Invoke()

// This code example produces the following output:
//     An instance method closed over C.
//     Instance method M1 on C:  id = 42, s = Hello, World!
//     Instance method M1 on C:  id = 42, s = Hi, Mom!
//     
//     An open instance method.
//     Instance method M1 on C:  id = 42, s = Hello, World!
//     Instance method M1 on C:  id = 5280, s = Hi, Mom!
//     
//     An open static method.
//     Static method M2 on C:  s = Hello, World!
//     Static method M2 on C:  s = Hi, Mom!
//     
//     A static method closed over the first argument (String).
//     Static method M2 on C:  s = Hello, World!
Imports System.Reflection
Imports System.Security.Permissions

' Declare three delegate types for demonstrating the combinations
' of Shared versus instance methods and open versus closed
' delegates.
'
Public Delegate Sub D1(ByVal c As C2, ByVal s As String)
Public Delegate Sub D2(ByVal s As String)
Public Delegate Sub D3()

' A sample class with an instance method and a Shared method.
'
Public Class C2
    Private id As Integer
    Public Sub New(ByVal id As Integer)
        Me.id = id
    End Sub

    Public Sub M1(ByVal s As String)
        Console.WriteLine("Instance method M1 on C2:  id = {0}, s = {1}",
            Me.id, s)
    End Sub

    Public Shared Sub M2(ByVal s As String)
        Console.WriteLine("Shared method M2 on C2:  s = {0}", s)
    End Sub
End Class

Public Class Example2

    Public Shared Sub Main()

        Dim c1 As New C2(42)

        ' Get a MethodInfo for each method.
        '
        Dim mi1 As MethodInfo = GetType(C2).GetMethod("M1",
            BindingFlags.Public Or BindingFlags.Instance)
        Dim mi2 As MethodInfo = GetType(C2).GetMethod("M2",
            BindingFlags.Public Or BindingFlags.Static)

        Dim d1 As D1
        Dim d2 As D2
        Dim d3 As D3


        Console.WriteLine(vbLf & "An instance method closed over C2.")
        ' In this case, the delegate and the
        ' method must have the same list of argument types; use
        ' delegate type D2 with instance method M1.
        '
        Dim test As [Delegate] =
            [Delegate].CreateDelegate(GetType(D2), c1, mi1, False)

        ' Because False was specified for throwOnBindFailure 
        ' in the call to CreateDelegate, the variable 'test'
        ' contains Nothing if the method fails to bind (for 
        ' example, if mi1 happened to represent a method of 
        ' some class other than C2).
        '
        If test IsNot Nothing Then
            d2 = CType(test, D2)

            ' The same instance of C2 is used every time the
            ' delegate is invoked.
            d2("Hello, World!")
            d2("Hi, Mom!")
        End If


        Console.WriteLine(vbLf & "An open instance method.")
        ' In this case, the delegate has one more 
        ' argument than the instance method; this argument comes
        ' at the beginning, and represents the hidden instance
        ' argument of the instance method. Use delegate type D1
        ' with instance method M1.
        '
        d1 = CType([Delegate].CreateDelegate(GetType(D1), Nothing, mi1), D1)

        ' An instance of C2 must be passed in each time the 
        ' delegate is invoked.
        '
        d1(c1, "Hello, World!")
        d1(New C2(5280), "Hi, Mom!")


        Console.WriteLine(vbLf & "An open Shared method.")
        ' In this case, the delegate and the method must 
        ' have the same list of argument types; use delegate type
        ' D2 with Shared method M2.
        '
        d2 = CType([Delegate].CreateDelegate(GetType(D2), Nothing, mi2), D2)

        ' No instances of C2 are involved, because this is a Shared
        ' method. 
        '
        d2("Hello, World!")
        d2("Hi, Mom!")


        Console.WriteLine(vbLf & "A Shared method closed over the first argument (String).")
        ' The delegate must omit the first argument of the method.
        ' A string is passed as the firstArgument parameter, and 
        ' the delegate is bound to this string. Use delegate type 
        ' D3 with Shared method M2. 
        '
        d3 = CType([Delegate].CreateDelegate(GetType(D3), "Hello, World!", mi2), D3)

        ' Each time the delegate is invoked, the same string is
        ' used.
        d3()

    End Sub
End Class

' This code example produces the following output:
'
'An instance method closed over C2.
'Instance method M1 on C2:  id = 42, s = Hello, World!
'Instance method M1 on C2:  id = 42, s = Hi, Mom!
'
'An open instance method.
'Instance method M1 on C2:  id = 42, s = Hello, World!
'Instance method M1 on C2:  id = 5280, s = Hi, Mom!
'
'An open Shared method.
'Shared method M2 on C2:  s = Hello, World!
'Shared method M2 on C2:  s = Hi, Mom!
'
'A Shared method closed over the first argument (String).
'Shared method M2 on C2:  s = Hello, World!
'
例 2

次のコード例は、パラメーター型と戻り値の型の互換性を示しています。

このコード例では、Base という名前の基底クラスと、Derivedから派生する Base という名前のクラスを定義します。 派生クラスには、static 型のパラメーターとSharedの戻り値の型を持つ MyMethod という名前のBase (Visual Basic のDerived) メソッドがあります。 このコード例では、Example 型のパラメーターと Derived 型の戻り値を持つ Base という名前のデリゲートも定義されています。

このコード例では、 Example という名前のデリゲートを使用してメソッド MyMethodを表すことができることを示します。 メソッドは、次の理由でデリゲートにバインドできます。

  • デリゲートのパラメーター型 (Derived) は、 MyMethod (Base) のパラメーター型よりも制限が厳しく、デリゲートの引数を常に MyMethodに渡すことができます。
  • MyMethod (Derived) の戻り値の型は、デリゲート (Base) のパラメーター型よりも制限が厳しく、メソッドの戻り値の型をデリゲートの戻り値の型にキャストしても常に安全です。

このコード例では、出力は生成されません。

using System;
using System.Reflection;

// Define two classes to use in the demonstration, a base class and
// a class that derives from it.
//
public class Base { }

public class Derived : Base
{
    // Define a static method to use in the demonstration. The method
    // takes an instance of Base and returns an instance of Derived.
    // For the purposes of the demonstration, it is not necessary for
    // the method to do anything useful.
    //
    public static Derived MyMethod(Base arg)
    {
        Base dummy = arg;
        return new Derived();
    }
}

// Define a delegate that takes an instance of Derived and returns an
// instance of Base.
//
public delegate Base Example5(Derived arg);

class Test
{
    public static void Main()
    {
        // The binding flags needed to retrieve MyMethod.
        BindingFlags flags = BindingFlags.Public | BindingFlags.Static;

        // Get a MethodInfo that represents MyMethod.
        MethodInfo minfo = typeof(Derived).GetMethod("MyMethod", flags);

        // Demonstrate contravariance of parameter types and covariance
        // of return types by using the delegate Example5 to represent
        // MyMethod. The delegate binds to the method because the
        // parameter of the delegate is more restrictive than the
        // parameter of the method (that is, the delegate accepts an
        // instance of Derived, which can always be safely passed to
        // a parameter of type Base), and the return type of MyMethod
        // is more restrictive than the return type of Example5 (that
        // is, the method returns an instance of Derived, which can
        // always be safely cast to type Base).
        //
        Example5 ex =
            (Example5)Delegate.CreateDelegate(typeof(Example5), minfo);

        // Execute MyMethod using the delegate Example5.
        //
        Base b = ex(new Derived());
    }
}
open System
open System.Reflection

// Define two classes to use in the demonstration, a base class and
// a class that derives from it.
type Base() = class end

type Derived() =
    inherit Base()

    // Define a static method to use in the demonstration. The method
    // takes an instance of Base and returns an instance of Derived.
    // For the purposes of the demonstration, it is not necessary for
    // the method to do anything useful.
    static member MyMethod(arg: Base) =
        Derived()

// Define a delegate that takes an instance of Derived and returns an
// instance of Base.
type Example = delegate of Derived -> Base

// The binding flags needed to retrieve MyMethod.
let flags = BindingFlags.Public ||| BindingFlags.Static

// Get a MethodInfo that represents MyMethod.
let minfo = typeof<Derived>.GetMethod("MyMethod", flags)

// Demonstrate contravariance of parameter types and covariance
// of return types by using the delegate Example to represent
// MyMethod. The delegate binds to the method because the
// parameter of the delegate is more restrictive than the
// parameter of the method (that is, the delegate accepts an
// instance of Derived, which can always be safely passed to
// a parameter of type Base), and the return type of MyMethod
// is more restrictive than the return type of Example (that
// is, the method returns an instance of Derived, which can
// always be safely cast to type Base).
let ex = Delegate.CreateDelegate(typeof<Example>, minfo) :?> Example

// Execute MyMethod using the delegate Example.
let b = Derived() |> ex.Invoke
Imports System.Reflection

' Define two classes to use in the demonstration, a base class and 
' a class that derives from it.
'
Public Class Base
End Class

Public Class Derived
    Inherits Base

    ' Define a Shared method to use in the demonstration. The method 
    ' takes an instance of Base and returns an instance of Derived.  
    ' For the purposes of the demonstration, it is not necessary for 
    ' the method to do anything useful. 
    '
    Public Shared Function MyMethod(ByVal arg As Base) As Derived
        Dim dummy As Base = arg
        Return New Derived()
    End Function

End Class

' Define a delegate that takes an instance of Derived and returns an
' instance of Base.
'
Public Delegate Function Example(ByVal arg As Derived) As Base

Module Test

    Sub Main()

        ' The binding flags needed to retrieve MyMethod.
        Dim flags As BindingFlags = _
            BindingFlags.Public Or BindingFlags.Static

        ' Get a MethodInfo that represents MyMethod.
        Dim minfo As MethodInfo = _
            GetType(Derived).GetMethod("MyMethod", flags)

        ' Demonstrate contravariance of parameter types and covariance
        ' of return types by using the delegate Example to represent
        ' MyMethod. The delegate binds to the method because the
        ' parameter of the delegate is more restrictive than the 
        ' parameter of the method (that is, the delegate accepts an
        ' instance of Derived, which can always be safely passed to
        ' a parameter of type Base), and the return type of MyMethod
        ' is more restrictive than the return type of Example (that
        ' is, the method returns an instance of Derived, which can
        ' always be safely cast to type Base). 
        '
        Dim ex As Example = CType( _
            [Delegate].CreateDelegate(GetType(Example), minfo), _
            Example _
        )

        ' Execute MyMethod using the delegate Example.
        '        
        Dim b As Base = ex(New Derived())
    End Sub
End Module

CreateDelegate(Type, Object, MethodInfo) および CreateDelegate(Type, Object, MethodInfo, Boolean) メソッド。

これら 2 つのオーバーロードの機能は同じですが、1 つはバインドに失敗した場合にスローするかどうかを指定でき、もう 1 つは常にスローします。

デリゲート型とメソッドには互換性のある戻り値の型が必要です。 つまり、 method の戻り値の型は、 typeの戻り値の型に割り当て可能である必要があります。

firstArgument、これらのオーバーロードの 2 番目のパラメーターは、デリゲートが表すメソッドの最初の引数です。 firstArgumentが指定されると、デリゲートが呼び出されるたびにmethodに渡されます。firstArgumentはデリゲートにバインドされ、デリゲートは最初の引数で閉じられると言われます。 methodstaticされている場合 (Visual Basic ではShared)、デリゲートの呼び出し時に指定された引数リストには、最初のパラメーターを除くすべてのパラメーターが含まれます。methodがインスタンス メソッドの場合、firstArgumentは非表示のインスタンス パラメーター (C# でthisまたは Visual Basic でMeによって表されます) に渡されます。

firstArgumentを指定する場合、methodの最初のパラメーターは参照型である必要があり、firstArgumentはその型と互換性がある必要があります。

Important

methodstatic (Visual Basic ではShared) で、最初のパラメーターの型が Object または ValueType の場合は、firstArgument値型を指定できます。 この場合、 firstArgument は自動的にボックス化されます。 自動ボックス化は、C# または Visual Basic 関数呼び出しの場合と同様に、他の引数では発生しません。

firstArgumentが null 参照で、methodがインスタンス メソッドの場合、結果はデリゲート型のtypeおよびmethodのシグネチャによって異なります。

  • typeのシグネチャにmethodの非表示の最初のパラメーターが明示的に含まれている場合、デリゲートは開いているインスタンス メソッドを表すと言われます。 デリゲートが呼び出されると、引数リストの最初の引数が methodの非表示のインスタンス パラメーターに渡されます。
  • methodtypeのシグネチャが一致する場合 (つまり、すべてのパラメーター型に互換性がある)、デリゲートは null 参照で閉じられると言われます。 デリゲートの呼び出しは、null インスタンスでインスタンス メソッドを呼び出すことと似ていますが、これは特に便利なことではありません。

firstArgumentが null 参照で、methodが静的な場合、結果はデリゲート型のtypeおよびmethodのシグネチャによって異なります。

  • methodtypeのシグネチャが一致する場合 (つまり、すべてのパラメーター型に互換性がある)、デリゲートはオープン静的メソッドを表すと言います。 これは、静的メソッドの最も一般的なケースです。 この場合、 CreateDelegate(Type, MethodInfo) メソッドのオーバーロードを使用すると、パフォーマンスが若干向上します。
  • typeのシグネチャが method の 2 番目のパラメーターで始まり、残りのパラメーター型に互換性がある場合、デリゲートは null 参照で閉じられると言われます。 デリゲートが呼び出されると、null 参照が method の最初のパラメーターに渡されます。

次のコード例は、1 つのデリゲート型が表すことができるすべてのメソッドを示しています。インスタンス メソッドで閉じる、インスタンス メソッドで開く、静的メソッドで開く、静的メソッドで閉じるなどです。

このコード例では、CFの 2 つのクラスと、D型の 1 つの引数を持つデリゲート型Cを定義します。 クラスには、一致する静的メソッドとインスタンス メソッド M1M3、および M4があり、クラス C には引数のないインスタンス メソッド M2 もあります。

Exampleという名前の 3 番目のクラスには、デリゲートを作成するコードが含まれています。

  • デリゲートは、型M1と型Cのインスタンス メソッドFに対して作成されます。各デリゲートは、それぞれの型のインスタンスによって閉じられます。 M1型のメソッド Cは、バインドされたインスタンスと引数のIDプロパティを表示します。
  • M2型のメソッドCに対してデリゲートが作成されます。 これは、デリゲートの引数がインスタンス メソッドの非表示の最初の引数を表す、開いているインスタンス デリゲートです。 このメソッドには他の引数はありません。 静的メソッドであるかのように呼び出されます。
  • デリゲートは、M3型と型Cの静的メソッドF用に作成されます。これらは、オープンな静的デリゲートです。
  • 最後に、デリゲートは、M4型と型Cの静的メソッドFに対して作成されます。各メソッドは最初の引数として宣言型を持ち、型のインスタンスが指定されるため、デリゲートは最初の引数で閉じられます。 M4型のメソッド Cは、バインドされたインスタンスと引数のIDプロパティを表示します。
using System;
using System.Reflection;

// Declare a delegate type. The object of this code example
// is to show all the methods this delegate can bind to.
//
public delegate void D(C1 c);

// Declare two sample classes, C1 and F. Class C1 has an ID
// property so instances can be identified.
//
public class C1
{
    private int id;
    public int ID => id;
    public C1(int id) => this.id = id;

    public void M1(C1 c)
    {
        Console.WriteLine("Instance method M1(C1 c) on C1:  this.id = {0}, c.ID = {1}",
            this.id, c.ID);
    }

    public void M2()
    {
        Console.WriteLine($"Instance method M2() on C1:  this.id = {this.id}");
    }

    public static void M3(C1 c)
    {
        Console.WriteLine($"Static method M3(C1 c) on C1:  c.ID = {c.ID}");
    }

    public static void M4(C1 c1, C1 c2)
    {
        Console.WriteLine("Static method M4(C1 c1, C1 c2) on C1:  c1.ID = {0}, c2.ID = {1}",
            c1.ID, c2.ID);
    }
}

public class F
{
    public void M1(C1 c)
    {
        Console.WriteLine($"Instance method M1(C1 c) on F:  c.ID = {c.ID}");
    }

    public static void M3(C1 c)
    {
        Console.WriteLine($"Static method M3(C1 c) on F:  c.ID = {c.ID}");
    }

    public static void M4(F f, C1 c)
    {
        Console.WriteLine($"Static method M4(F f, C1 c) on F:  c.ID = {c.ID}");
    }
}

public class Example
{
    public static void Main()
    {
        C1 c1 = new (42);
        C1 c2 = new (1491);
        F f1 = new ();

        D d;

        // Instance method with one argument of type C1.
        MethodInfo cmi1 = typeof(C1).GetMethod("M1");
        // Instance method with no arguments.
        MethodInfo cmi2 = typeof(C1).GetMethod("M2");
        // Static method with one argument of type C1.
        MethodInfo cmi3 = typeof(C1).GetMethod("M3");
        // Static method with two arguments of type C1.
        MethodInfo cmi4 = typeof(C1).GetMethod("M4");

        // Instance method with one argument of type C1.
        MethodInfo fmi1 = typeof(F).GetMethod("M1");
        // Static method with one argument of type C1.
        MethodInfo fmi3 = typeof(F).GetMethod("M3");
        // Static method with an argument of type F and an argument
        // of type C1.
        MethodInfo fmi4 = typeof(F).GetMethod("M4");

        Console.WriteLine("\nAn instance method on any type, with an argument of type C1.");
        // D can represent any instance method that exactly matches its
        // signature. Methods on C1 and F are shown here.
        //
        d = (D)Delegate.CreateDelegate(typeof(D), c1, cmi1);
        d(c2);
        d = (D)Delegate.CreateDelegate(typeof(D), f1, fmi1);
        d(c2);

        Console.WriteLine("\nAn instance method on C1 with no arguments.");
        // D can represent an instance method on C1 that has no arguments;
        // in this case, the argument of D represents the hidden first
        // argument of any instance method. The delegate acts like a
        // static method, and an instance of C1 must be passed each time
        // it is invoked.
        //
        d = (D)Delegate.CreateDelegate(typeof(D), null, cmi2);
        d(c1);

        Console.WriteLine("\nA static method on any type, with an argument of type C1.");
        // D can represent any static method with the same signature.
        // Methods on F and C1 are shown here.
        //
        d = (D)Delegate.CreateDelegate(typeof(D), null, cmi3);
        d(c1);
        d = (D)Delegate.CreateDelegate(typeof(D), null, fmi3);
        d(c1);

        Console.WriteLine("\nA static method on any type, with an argument of");
        Console.WriteLine("    that type and an argument of type C1.");
        // D can represent any static method with one argument of the
        // type the method belongs and a second argument of type C1.
        // In this case, the method is closed over the instance of
        // supplied for the its first argument, and acts like an instance
        // method. Methods on F and C1 are shown here.
        //
        d = (D)Delegate.CreateDelegate(typeof(D), c1, cmi4);
        d(c2);
        Delegate test =
            Delegate.CreateDelegate(typeof(D), f1, fmi4, false);

        // This final example specifies false for throwOnBindFailure
        // in the call to CreateDelegate, so the variable 'test'
        // contains Nothing if the method fails to bind (for
        // example, if fmi4 happened to represent a method of
        // some class other than F).
        //
        if (test != null)
        {
            d = (D)test;
            d(c2);
        }
    }
}

/* This code example produces the following output:

An instance method on any type, with an argument of type C1.
Instance method M1(C1 c) on C1:  this.id = 42, c.ID = 1491
Instance method M1(C1 c) on F:  c.ID = 1491

An instance method on C1 with no arguments.
Instance method M2() on C1:  this.id = 42

A static method on any type, with an argument of type C1.
Static method M3(C1 c) on C1:  c.ID = 42
Static method M3(C1 c) on F:  c.ID = 42

A static method on any type, with an argument of
    that type and an argument of type C1.
Static method M4(C1 c1, C1 c2) on C1:  c1.ID = 42, c2.ID = 1491
Static method M4(F f, C1 c) on F:  c.ID = 1491
*/
open System

// Declare two sample classes, C and F. Class C has an ID
// property so instances can be identified.
type C(id) =
    member _.ID = id 

    member _.M1(c: C) =
        printfn $"Instance method M1(C c) on C:  this.id = {id}, c.ID = {c.ID}"

    member _.M2() =
        printfn $"Instance method M2() on C:  this.id = {id}"

    static member M3(c: C) =
        printfn $"Static method M3(C c) on C:  c.ID = {c.ID}"

    static member M4(c1: C, c2: C) =
        printfn $"Static method M4(C c1, C c2) on C:  c1.ID = {c1.ID}, c2.ID = {c2.ID}"

// Declare a delegate type. The object of this code example
// is to show all the methods this delegate can bind to.
type D = delegate of C -> unit


type F() =
    member _.M1(c: C) =
        printfn $"Instance method M1(C c) on F:  c.ID = {c.ID}"

    member _.M3(c: C) =
        printfn $"Static method M3(C c) on F:  c.ID = {c.ID}"

    member _.M4(f: F, c: C) =
        printfn $"Static method M4(F f, C c) on F:  c.ID = {c.ID}"

[<EntryPoint>]
let main _ =
    let c1 = C 42
    let c2 = C 1491
    let f1 = F()

    // Instance method with one argument of type C.
    let cmi1 = typeof<C>.GetMethod "M1"
    // Instance method with no arguments.
    let cmi2 = typeof<C>.GetMethod "M2"
    // Static method with one argument of type C.
    let cmi3 = typeof<C>.GetMethod "M3"
    // Static method with two arguments of type C.
    let cmi4 = typeof<C>.GetMethod "M4"

    // Instance method with one argument of type C.
    let fmi1 = typeof<F>.GetMethod "M1"
    // Static method with one argument of type C.
    let fmi3 = typeof<F>.GetMethod "M3"
    // Static method with an argument of type F and an argument
    // of type C.
    let fmi4 = typeof<F>.GetMethod "M4"

    printfn "\nAn instance method on any type, with an argument of type C."
    // D can represent any instance method that exactly matches its
    // signature. Methods on C and F are shown here.
    let d = Delegate.CreateDelegate(typeof<D>, c1, cmi1) :?> D
    d.Invoke c2
    let d =  Delegate.CreateDelegate(typeof<D>, f1, fmi1) :?> D
    d.Invoke c2

    Console.WriteLine("\nAn instance method on C with no arguments.")
    // D can represent an instance method on C that has no arguments
    // in this case, the argument of D represents the hidden first
    // argument of any instance method. The delegate acts like a
    // static method, and an instance of C must be passed each time
    // it is invoked.
    let d = Delegate.CreateDelegate(typeof<D>, null, cmi2) :?> D
    d.Invoke c1

    printfn "\nA static method on any type, with an argument of type C."
    // D can represent any static method with the same signature.
    // Methods on F and C are shown here.
    let d = Delegate.CreateDelegate(typeof<D>, null, cmi3) :?> D
    d.Invoke c1
    let d = Delegate.CreateDelegate(typeof<D>, null, fmi3) :?> D
    d.Invoke c1

    printfn "\nA static method on any type, with an argument of"
    printfn "    that type and an argument of type C."
    // D can represent any static method with one argument of the
    // type the method belongs and a second argument of type C.
    // In this case, the method is closed over the instance of
    // supplied for the its first argument, and acts like an instance
    // method. Methods on F and C are shown here.
    let d = Delegate.CreateDelegate(typeof<D>, c1, cmi4) :?> D
    d.Invoke c2
    let test =
        Delegate.CreateDelegate(typeof<D>, f1, fmi4, false)

    // This final example specifies false for throwOnBindFailure
    // in the call to CreateDelegate, so the variable 'test'
    // contains Nothing if the method fails to bind (for
    // example, if fmi4 happened to represent a method of
    // some class other than F).
    match test with
    | :? D as d ->
        d.Invoke c2
    | _ -> ()
    0

// This code example produces the following output:
//     An instance method on any type, with an argument of type C.
//     Instance method M1(C c) on C:  this.id = 42, c.ID = 1491
//     Instance method M1(C c) on F:  c.ID = 1491
//    
//     An instance method on C with no arguments.
//     Instance method M2() on C:  this.id = 42
//    
//     A static method on any type, with an argument of type C.
//     Static method M3(C c) on C:  c.ID = 42
//     Static method M3(C c) on F:  c.ID = 42
//    
//     A static method on any type, with an argument of
//         that type and an argument of type C.
//     Static method M4(C c1, C c2) on C:  c1.ID = 42, c2.ID = 1491
//     Static method M4(F f, C c) on F:  c.ID = 1491
Imports System.Reflection
Imports System.Security.Permissions

' Declare a delegate type. The object of this code example
' is to show all the methods this delegate can bind to.
'
Public Delegate Sub D(ByVal c As C) 

' Declare two sample classes, C and F. Class C has an ID
' property so instances can be identified.
'
Public Class C

    Private _id As Integer

    Public ReadOnly Property ID() As Integer 
        Get
            Return _id
        End Get
    End Property

    Public Sub New(ByVal newId As Integer) 
        Me._id = newId
    End Sub
    
    Public Sub M1(ByVal c As C) 
        Console.WriteLine("Instance method M1(c As C) on C:  this.id = {0}, c.ID = {1}", _
            Me.id, c.ID)
    End Sub
    
    Public Sub M2() 
        Console.WriteLine("Instance method M2() on C:  this.id = {0}", Me.id)
    End Sub
    
    Public Shared Sub M3(ByVal c As C) 
        Console.WriteLine("Shared method M3(c As C) on C:  c.ID = {0}", c.ID)
    End Sub
    
    Public Shared Sub M4(ByVal c1 As C, ByVal c2 As C) 
        Console.WriteLine("Shared method M4(c1 As C, c2 As C) on C:  c1.ID = {0}, c2.ID = {1}", _
            c1.ID, c2.ID)
    End Sub
End Class


Public Class F
    
    Public Sub M1(ByVal c As C) 
        Console.WriteLine("Instance method M1(c As C) on F:  c.ID = {0}", c.ID)
    End Sub
    
    Public Shared Sub M3(ByVal c As C) 
        Console.WriteLine("Shared method M3(c As C) on F:  c.ID = {0}", c.ID)
    End Sub
    
    Public Shared Sub M4(ByVal f As F, ByVal c As C) 
        Console.WriteLine("Shared method M4(f As F, c As C) on F:  c.ID = {0}", c.ID)
    End Sub
End Class

Public Class Example5

    Public Shared Sub Main()

        Dim c1 As New C(42)
        Dim c2 As New C(1491)
        Dim f1 As New F()

        Dim d As D

        ' Instance method with one argument of type C.
        Dim cmi1 As MethodInfo = GetType(C).GetMethod("M1")
        ' Instance method with no arguments.
        Dim cmi2 As MethodInfo = GetType(C).GetMethod("M2")
        ' Shared method with one argument of type C.
        Dim cmi3 As MethodInfo = GetType(C).GetMethod("M3")
        ' Shared method with two arguments of type C.
        Dim cmi4 As MethodInfo = GetType(C).GetMethod("M4")

        ' Instance method with one argument of type C.
        Dim fmi1 As MethodInfo = GetType(F).GetMethod("M1")
        ' Shared method with one argument of type C.
        Dim fmi3 As MethodInfo = GetType(F).GetMethod("M3")
        ' Shared method with an argument of type F and an 
        ' argument of type C.
        Dim fmi4 As MethodInfo = GetType(F).GetMethod("M4")

        Console.WriteLine(vbLf & "An instance method on any type, with an argument of type C.")
        ' D can represent any instance method that exactly matches its
        ' signature. Methods on C and F are shown here.
        '
        d = CType([Delegate].CreateDelegate(GetType(D), c1, cmi1), D)
        d(c2)
        d = CType([Delegate].CreateDelegate(GetType(D), f1, fmi1), D)
        d(c2)

        Console.WriteLine(vbLf & "An instance method on C with no arguments.")
        ' D can represent an instance method on C that has no arguments;
        ' in this case, the argument of D represents the hidden first
        ' argument of any instance method. The delegate acts like a 
        ' Shared method, and an instance of C must be passed each time
        ' it is invoked.
        '
        d = CType([Delegate].CreateDelegate(GetType(D), Nothing, cmi2), D)
        d(c1)

        Console.WriteLine(vbLf & "A Shared method on any type, with an argument of type C.")
        ' D can represent any Shared method with the same signature.
        ' Methods on F and C are shown here.
        '
        d = CType([Delegate].CreateDelegate(GetType(D), Nothing, cmi3), D)
        d(c1)
        d = CType([Delegate].CreateDelegate(GetType(D), Nothing, fmi3), D)
        d(c1)

        Console.WriteLine(vbLf & "A Shared method on any type, with an argument of")
        Console.WriteLine("    that type and an argument of type C.")
        ' D can represent any Shared method with one argument of the
        ' type the method belongs and a second argument of type C.
        ' In this case, the method is closed over the instance of
        ' supplied for the its first argument, and acts like an instance
        ' method. Methods on F and C are shown here.
        '
        d = CType([Delegate].CreateDelegate(GetType(D), c1, cmi4), D)
        d(c2)
        Dim test As [Delegate] =
            [Delegate].CreateDelegate(GetType(D), f1, fmi4, False)

        ' This final example specifies False for throwOnBindFailure 
        ' in the call to CreateDelegate, so the variable 'test'
        ' contains Nothing if the method fails to bind (for 
        ' example, if fmi4 happened to represent a method of  
        ' some class other than F).
        '
        If test IsNot Nothing Then
            d = CType(test, D)
            d(c2)
        End If

    End Sub
End Class

' This code example produces the following output:
'
'An instance method on any type, with an argument of type C.
'Instance method M1(c As C) on C:  this.id = 42, c.ID = 1491
'Instance method M1(c As C) on F:  c.ID = 1491
'
'An instance method on C with no arguments.
'Instance method M2() on C:  this.id = 42
'
'A Shared method on any type, with an argument of type C.
'Shared method M3(c As C) on C:  c.ID = 42
'Shared method M3(c As C) on F:  c.ID = 42
'
'A Shared method on any type, with an argument of
'    that type and an argument of type C.
'Shared method M4(c1 As C, c2 As C) on C:  c1.ID = 42, c2.ID = 1491
'Shared method M4(f As F, c As C) on F:  c.ID = 1491
'

互換性のあるパラメーター型と戻り値の型

このメソッド オーバーロードを使用して作成されたデリゲートのパラメーター型と戻り値の型は、デリゲートが表すメソッドのパラメーター型と戻り値の型と互換性がある必要があります。型が正確に一致する必要はありません。

デリゲートパラメーターの型がメソッド パラメーターの型よりも制限が厳しい場合、デリゲートのパラメーターはメソッドの対応するパラメーターと互換性があります。これは、デリゲートに渡される引数をメソッドに安全に渡すことができるためです。

同様に、メソッドの戻り値の型がデリゲートの戻り値の型よりも制限が厳しい場合、デリゲートの戻り値の型はメソッドの戻り値の型と互換性があります。これは、メソッドの戻り値をデリゲートの戻り値の型に安全にキャストできることを保証するためです。

たとえば、 Hashtable 型のパラメーターを持つデリゲートと Object の戻り値の型は、 Object 型のパラメーターと Hashtable型の戻り値を持つメソッドを表すことができます。

デリゲートが表すことができるメソッドを決定する

CreateDelegate(Type, Object, MethodInfo)オーバーロードによって提供される柔軟性を考えるもう 1 つの便利な方法は、任意のデリゲートがメソッド シグネチャとメソッドの種類 (静的とインスタンス) の 4 つの異なる組み合わせを表すことができるということです。 D型の 1 つの引数を持つデリゲート型Cについて考えます。 次に、すべてのケースで一致する必要があるため、戻り値の型を無視して、 D 表すことができるメソッドについて説明します。

  • D は、インスタンス メソッドが属する型に関係なく、 C型の引数を 1 つだけ持つ任意のインスタンス メソッドを表すことができます。 CreateDelegateが呼び出されると、firstArgumentmethodが属する型のインスタンスであり、結果のデリゲートはそのインスタンスで閉じられると言われます。 (簡単に言うと、Dが null 参照の場合は、firstArgumentを null 参照で閉じることもできます)。

  • D は、引数を持たない C のインスタンス メソッドを表すことができます。 CreateDelegateが呼び出されると、firstArgumentは null 参照になります。 結果のデリゲートは開いているインスタンス メソッドを表し、呼び出されるたびに C のインスタンスを指定する必要があります。

  • D は、 C型の 1 つの引数を受け取る静的メソッドを表し、そのメソッドは任意の型に属することができます。 CreateDelegateが呼び出されると、firstArgumentは null 参照になります。 結果のデリゲートは、開いている静的メソッドを表し、呼び出されるたびに C のインスタンスを指定する必要があります。

  • D は、型 F に属し、 F 型と C型の 2 つの引数を持つ静的メソッドを表すことができます。 CreateDelegateが呼び出されると、firstArgumentFのインスタンスになります。 結果のデリゲートは、 Fのそのインスタンスで閉じる静的メソッドを表します。 FCが同じ型である場合、静的メソッドにはその型の 2 つの引数があることに注意してください。 この場合、Dが null 参照であるとき、firstArgumentは null 参照に対して閉じられます。

注釈

Delegate クラスは、デリゲート型の基底クラスです。 ただし、 Delegate クラスまたは MulticastDelegate クラスから明示的に派生できるのは、システムとコンパイラだけです。 また、デリゲート型から新しい型を派生させることもできます。 Delegate クラスはデリゲート型とは見なされません。デリゲート型を派生させるために使用されるクラスです。

ほとんどの言語は delegate キーワードを実装し、それらの言語のコンパイラは MulticastDelegate クラスから派生できるため、ユーザーは言語によって提供される delegate キーワードを使用する必要があります。

Note

共通言語ランタイムは、デリゲートと同じシグネチャを持つデリゲート型ごとに Invoke メソッドを提供します。 コンパイラによって自動的に呼び出されるため、C# または Visual Basic から明示的にこのメソッドを呼び出す必要はありません。 Invoke メソッドは、デリゲート型のシグネチャを見つける場合にリフレクションに役立ちます。

共通言語ランタイムは、デリゲートの非同期呼び出しを有効にするために、各デリゲート型に BeginInvoke メソッドと EndInvoke メソッドを提供します。 これらのメソッドの詳細については、「 同期メソッドの非同期呼び出し」を参照してください。

デリゲート型の宣言は、1 つ以上のメソッドのシグネチャを指定するコントラクトを確立します。 デリゲートは、次への参照を持つデリゲート型のインスタンスです。

  • 型のインスタンス メソッドと、その型に割り当て可能なターゲット オブジェクト。
  • 仮パラメーター リストで公開されている非表示の this パラメーターを持つ型のインスタンス メソッド。 デリゲートは、開いているインスタンス デリゲートと言われます。
  • 静的メソッド。
  • 静的メソッドと、メソッドの最初のパラメーターに割り当て可能なターゲット オブジェクト。 デリゲートは、最初の引数で閉じられると言われます。

デリゲート バインドの詳細については、 CreateDelegate(Type, Object, MethodInfo, Boolean) メソッドのオーバーロードを参照してください。

デリゲートが最初の引数 (最も一般的なケース) で閉じたインスタンス メソッドを表す場合、デリゲートはメソッドのエントリ ポイントへの参照と、ターゲットと呼ばれるオブジェクトへの参照を格納します。これは、メソッドを定義した型に割り当て可能な型です。 デリゲートは、開いているインスタンス メソッドを表す場合、メソッドのエントリ ポイントへの参照を格納します。 デリゲートシグネチャは、仮パラメーター リストに非表示の this パラメーターを含める必要があります。この場合、デリゲートはターゲット オブジェクトへの参照を持たないため、デリゲートが呼び出されたときにターゲット オブジェクトを指定する必要があります。

デリゲートが静的メソッドを表す場合、デリゲートはメソッドのエントリ ポイントへの参照を格納します。 デリゲートが最初の引数で閉じた静的メソッドを表す場合、デリゲートはメソッドのエントリ ポイントへの参照と、メソッドの最初の引数の型に割り当て可能なターゲット オブジェクトへの参照を格納します。 デリゲートが呼び出されると、静的メソッドの最初の引数はターゲット オブジェクトを受け取ります。 この最初の引数は参照型である必要があります。

デリゲートの呼び出しリストは、一覧の各要素がデリゲートによって表されるメソッドの 1 つを呼び出す、順序付けられたデリゲートのセットです。 呼び出しリストには、重複するメソッドを含めることができます。 呼び出し中、メソッドは呼び出しリストに表示される順序で呼び出されます。 デリゲートは、その呼び出しリスト内のすべてのメソッドを呼び出そうとします。重複は、呼び出しリストに表示されるたびに 1 回呼び出されます。 デリゲートは不変です。作成されると、デリゲートの呼び出しリストは変更されません。

デリゲートは 1 つ以上のメソッドを呼び出し、操作の組み合わせで使用できるため、デリゲートはマルチキャストまたは結合可能と呼ばれます。

CombineRemoveなどの操作を組み合わせると、既存のデリゲートは変更されません。 代わりに、このような操作は、操作の結果、変更されていないデリゲート、または nullを含む新しいデリゲートを返します。 結合操作は、操作の結果が少なくとも 1 つのメソッドを参照しないデリゲートである場合、 null を返します。 結合操作は、要求された操作に影響がない場合に、変更されていないデリゲートを返します。

Note

マネージド言語では、 Combine メソッドと Remove メソッドを使用してデリゲート操作を実装します。 たとえば、Visual Basic の AddHandler ステートメントと RemoveHandler ステートメント、C# のデリゲート型の += 演算子と -= 演算子などがあります。

ジェネリック デリゲート型には、バリアント型パラメーターを指定できます。 反変型パラメーターはデリゲートのパラメーター型として使用でき、共変型パラメーターは戻り値の型として使用できます。 この機能により、同じジェネリック型定義から構築されたジェネリック デリゲート型は、共 変性と反変性で説明されているように、型引数が継承関係を持つ参照型である場合に割り当て互換性があります。

Note

分散のために割り当て互換であるジェネリック デリゲートは、必ずしも組み合わせ可能ではありません。 組み合わせ可能にするには、型が正確に一致する必要があります。 たとえば、 Derived という名前のクラスが、 Baseという名前のクラスから派生しているとします。 型 Action<Base> (Visual Basic の Action(Of Base)) のデリゲートは、Action<Derived> 型の変数に割り当てることができますが、型が完全に一致しないため、2 つのデリゲートを結合することはできません。

呼び出されたメソッドが例外をスローした場合、メソッドは実行を停止し、例外はデリゲートの呼び出し元に返され、呼び出しリスト内の残りのメソッドは呼び出されません。 呼び出し元で例外をキャッチしても、この動作は変更されません。

デリゲートによって呼び出されたメソッドのシグネチャに戻り値が含まれている場合、デリゲートは呼び出しリストの最後の要素の戻り値を返します。 参照渡しされるパラメーターがシグネチャに含まれている場合、パラメーターの最終的な値は、呼び出しリスト内のすべてのメソッドが順番に実行され、パラメーターの値が更新された結果になります。

C のデリゲートに最も近いのは、関数ポインターです。 デリゲートは、静的メソッドまたはインスタンス メソッドを表すことができます。 デリゲートがインスタンス メソッドを表す場合、デリゲートはメソッドのエントリ ポイントへの参照だけでなく、クラス インスタンスへの参照も格納します。 関数ポインターとは異なり、デリゲートはオブジェクト指向であり、型セーフです。

CreateDelegate の例

CreateDelegate メソッドは、指定した型のデリゲートを作成します。

CreateDelegate(Type, MethodInfo) メソッド

このメソッド オーバーロードは、CreateDelegate(Type, MethodInfo, Boolean) メソッドのオーバーロードを呼び出し、truethrowOnBindFailureを指定することと同じです。

コンストラクター

名前 説明
Delegate(Object, String)

指定したクラス インスタンスで指定したインスタンス メソッドを呼び出すデリゲートを初期化します。

Delegate(Type, String)

指定したクラスから指定した静的メソッドを呼び出すデリゲートを初期化します。

プロパティ

名前 説明
HasSingleTarget

Delegateに単一の呼び出しターゲットがあるかどうかを示す値を取得します。

Method

デリゲートによって表されるメソッドを取得します。

Target

現在のデリゲートがインスタンス メソッドを呼び出すクラス インスタンスを取得します。

メソッド

名前 説明
Clone()

デリゲートの簡易コピーを作成します。

Combine(Delegate, Delegate)

2 つのデリゲートの呼び出しリストを連結します。

Combine(Delegate[])

デリゲートの配列の呼び出しリストを連結します。

Combine(ReadOnlySpan<Delegate>)

デリゲートのスパンの呼び出しリストを連結します。

CombineImpl(Delegate)

指定されたマルチキャスト (結合可能) デリゲートと現在のマルチキャスト (結合可能) デリゲートの呼び出しリストを連結します。

CreateDelegate(Type, MethodInfo, Boolean)

指定した静的メソッドを表す、指定した型のデリゲートを作成します。バインドに失敗した場合の動作を指定します。

CreateDelegate(Type, MethodInfo)

指定したメソッドを表す、指定した型のデリゲートを作成します。

CreateDelegate(Type, Object, MethodInfo, Boolean)

指定した静的メソッドまたはインスタンス メソッドを表す、指定した型のデリゲートを作成します。最初の引数を指定し、バインドに失敗した場合の動作を指定します。

CreateDelegate(Type, Object, MethodInfo)

指定した最初の引数を使用して、指定した静的メソッドまたはインスタンス メソッドを表す、指定した型のデリゲートを作成します。

CreateDelegate(Type, Object, String, Boolean, Boolean)

指定したクラス インスタンスで呼び出す、指定したインスタンス メソッドを表す、指定した型のデリゲートを作成します。バインドに失敗した場合は、指定した大文字と小文字の区別と指定した動作を使用します。

CreateDelegate(Type, Object, String, Boolean)

指定したクラス インスタンスで呼び出す指定したインスタンス メソッドを表す、指定した型のデリゲートを、指定した大文字と小文字を区別して作成します。

CreateDelegate(Type, Object, String)

指定したクラス インスタンスで呼び出す指定したインスタンス メソッドを表す、指定した型のデリゲートを作成します。

CreateDelegate(Type, Type, String, Boolean, Boolean)

指定したクラスの指定した静的メソッドを表す、指定した型のデリゲートを作成します。バインドに失敗した場合は、指定した大文字と小文字の区別と指定した動作を使用します。

CreateDelegate(Type, Type, String, Boolean)

指定したクラスの指定した静的メソッドを表す、指定した型のデリゲートを、指定した大文字と小文字を区別して作成します。

CreateDelegate(Type, Type, String)

指定したクラスの指定した静的メソッドを表す、指定した型のデリゲートを作成します。

DynamicInvoke(Object[])

現在のデリゲートによって表されるメソッドを動的に呼び出します (遅延バインド)。

DynamicInvokeImpl(Object[])

現在のデリゲートによって表されるメソッドを動的に呼び出します (遅延バインド)。

EnumerateInvocationList<TDelegate>(TDelegate)

このデリゲートの呼び出しターゲットの列挙子を取得します。

Equals(Object)

指定したオブジェクトと現在のデリゲートが同じ型であるかどうかを判断し、同じターゲット、メソッド、および呼び出しリストを共有します。

GetHashCode()

デリゲートのハッシュ コードを返します。

GetInvocationList()

デリゲートの呼び出しリストを返します。

GetMethodImpl()

現在のデリゲートによって表されるメソッドを取得します。

GetObjectData(SerializationInfo, StreamingContext)
古い.

サポートされていません。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
Remove(Delegate, Delegate)

別のデリゲートの呼び出しリストから、デリゲートの呼び出しリストの最後の出現箇所を削除します。

RemoveAll(Delegate, Delegate)

別のデリゲートの呼び出しリストから、デリゲートの呼び出しリストのすべての出現箇所を削除します。

RemoveImpl(Delegate)

別のデリゲートの呼び出しリストからデリゲートの呼び出しリストを削除します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

演算子

名前 説明
Equality(Delegate, Delegate)

指定したデリゲートが等しいかどうかを判断します。

Inequality(Delegate, Delegate)

指定したデリゲートが等しくないかどうかを判断します。

拡張メソッド

名前 説明
GetMethodInfo(Delegate)

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

適用対象

こちらもご覧ください