Delegate クラス
静的メソッドまたはクラス インスタンスとそのクラスのインスタンス メソッドを参照するデータ構造体であるデリゲートを表します。
この型のすべてのメンバの一覧については、Delegate メンバ を参照してください。
System.Object
System.Delegate
System.MulticastDelegate
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDual)>
MustInherit Public Class Delegate Implements ICloneable, ISerializable
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
public abstract class Delegate : ICloneable, ISerializable
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDual)]
public __gc __abstract class Delegate : public ICloneable, ISerializable
[JScript]
public
Serializable
ClassInterface(ClassInterfaceType.AutoDual)
abstract class Delegate implements ICloneable, ISerializable
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
Delegate クラスは、デリゲート型の基本クラスです。ただし、システムとコンパイラに限り、 Delegate クラスまたは MulticastDelegate クラスから明示的に派生させることができます。デリゲート型から新しい型を派生させることはできません。 Delegate クラスは、デリゲート型とは見なされません。このクラスは、デリゲート型を派生させるために使用されるクラスです。
ほとんどの言語では delegate キーワードが実装されています。また、それらの言語のコンパイラは MulticastDelegate クラスから派生させることができます。このため、開発者は各言語で提供されている delegate キーワードを使用する必要があります。
デリゲート型の宣言によって、1 つ以上のメソッドのシグネチャを指定するコントラクトを確立します。デリゲートはデリゲート型のインスタンスであり、次のいずれか 1 つ以上を参照します。
- null 参照 (Visual Basic では Nothing) ではない対象オブジェクトおよびその対象オブジェクトのインスタンス メソッド
- 静的メソッド
メソッドのシグネチャがデリゲート型で指定されたシグネチャと完全に一致する場合にだけ、デリゲートはメソッドを参照できます。デリゲートがインスタンス メソッドを参照している場合、デリゲートはそのメソッドのエントリ ポイントへの参照とオブジェクトへの参照を格納しています。この場合、オブジェクトはターゲットと呼ばれ、メソッドを呼び出す対象であるクラス インスタンスです。インスタンス メソッドのターゲットは null 参照 (Nothing) 以外である必要があります。デリゲートが静的メソッドを参照している場合、デリゲートはそのメソッドのエントリ ポイントへの参照を格納しています。静的メソッドのターゲットは null 参照 (Nothing) です。
デリゲートの呼び出しリストは、順序付けられたデリゲートのセットであり、呼び出しリスト内の各要素はデリゲートによって呼び出されるメソッドを 1 つずつ呼び出します。呼び出しリストには、重複メソッドを格納できます。デリゲートがメソッドを呼び出すときは、呼び出しリストに出現する順にメソッドを呼び出します。デリゲートは呼び出しリスト内のすべてのメソッドを呼び出そうとします。重複メソッドは、呼び出しリストに出現するたびに呼び出されます。デリゲートは不変であるため、デリゲートの呼び出しリストを一度作成すると、そのリストは変わりません。
デリゲートは、マルチキャスト (組み合わせ可能) またはシングルキャスト (組み合わせ不能) のどちらかです。マルチキャスト (組み合わせ可能) デリゲートは 1 つ以上のメソッドを呼び出すことができるため、組み合わせ演算で使用できます。シングルキャスト (組み合わせ不能) デリゲートはメソッドを 1 つだけ呼び出すため、組み合わせ演算では使用できません。シングルキャスト デリゲート A の呼び出しリストには、1 つの要素、つまり A への参照だけが格納されます。
Combine 、 Remove などの組み合わせ演算によって既存のデリゲートが変更されることはありません。その代わり、組み合わせ演算では、演算の結果、または変更されないデリゲート、または null 参照 (Nothing) が格納された新しいデリゲートが返されます。演算の結果がメソッドをまったく参照していないデリゲートの場合は、 null 参照 (Nothing) が返されます。要求した演算が無効の場合は、変更されないデリゲートが返されます。
呼び出されたメソッドが例外をスローした場合は、メソッドが実行を中止し、デリゲートの呼び出し元に例外が戻され、呼び出しリスト内の残りのメソッドは呼び出されません。呼び出し元で例外がキャッチされても、この動作は変わりません。
デリゲートが呼び出したメソッドのシグネチャに戻り値が含まれる場合、デリゲートは呼び出しリスト内の最後の要素の戻り値を返します。参照渡しされたパラメータがシグネチャに含まれる場合、そのパラメータの最終的な値は、呼び出しリスト内のすべてのメソッドが順次実行されたことによって更新された結果の値になります。
コンパイラは BeginInvoke と EndInvoke という 2 つの追加メソッドをデリゲートに提供します。これらのメソッドの詳細については、「 非同期プログラミングの概要 」を参照してください。
デリゲートに最も近い C または C++ の機能は関数ポインタです。ただし、関数ポインタは静的関数だけを参照できるのに対して、デリゲートは静的メソッドとインスタンス メソッドの両方を参照できます。デリゲートがインスタンス メソッドを参照している場合、デリゲートは、そのメソッドのエントリ ポイントへの参照だけでなく、メソッドを呼び出す対象のクラス インスタンスへの参照も格納しています。関数ポインタとは異なり、デリゲートはオブジェクト指向のタイプ セーフな機能です。
使用例
[Visual Basic, C#, C++] 標準デリゲートを定義する方法を次の例に示します。
Imports System
Public Class SamplesDelegate
' Declares a delegate for a method that takes in an int and returns a String.
Delegate Function myMethodDelegate(myInt As Integer) As [String]
' Defines some methods to which the delegate can point.
Public Class mySampleClass
' Defines an instance method.
Public Function myStringMethod(myInt As Integer) As [String]
If myInt > 0 Then
Return "positive"
End If
If myInt < 0 Then
Return "negative"
End If
Return "zero"
End Function 'myStringMethod
' Defines a static method.
Public Shared Function mySignMethod(myInt As Integer) As [String]
If myInt > 0 Then
Return "+"
End If
If myInt < 0 Then
Return "-"
End If
Return ""
End Function 'mySignMethod
End Class 'mySampleClass
Public Shared Sub Main()
' Creates one delegate for each method.
Dim mySC As New mySampleClass()
Dim myD1 As New myMethodDelegate(AddressOf mySC.myStringMethod)
Dim myD2 As New myMethodDelegate(AddressOf mySampleClass.mySignMethod)
' Invokes the delegates.
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 5, myD1(5), myD2(5))
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", - 3, myD1(- 3), myD2(- 3))
Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 0, myD1(0), myD2(0))
End Sub 'Main
End Class 'SamplesDelegate
'This code produces the following output:
'
'5 is positive; use the sign "+".
'-3 is negative; use the sign "-".
'0 is zero; use the sign "".
[C#]
using System;
public class SamplesDelegate {
// Declares a delegate for a method that takes in an int and returns a String.
public delegate String myMethodDelegate( int myInt );
// Defines some methods to which the delegate can point.
public class mySampleClass {
// Defines an instance method.
public String myStringMethod ( int myInt ) {
if ( myInt > 0 )
return( "positive" );
if ( myInt < 0 )
return( "negative" );
return ( "zero" );
}
// Defines a static method.
public static String mySignMethod ( int myInt ) {
if ( myInt > 0 )
return( "+" );
if ( myInt < 0 )
return( "-" );
return ( "" );
}
}
public static void Main() {
// Creates one delegate for each method.
mySampleClass mySC = new mySampleClass();
myMethodDelegate myD1 = new myMethodDelegate( mySC.myStringMethod );
myMethodDelegate myD2 = new myMethodDelegate( mySampleClass.mySignMethod );
// Invokes the delegates.
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", 5, myD1( 5 ), myD2( 5 ) );
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", -3, myD1( -3 ), myD2( -3 ) );
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", 0, myD1( 0 ), myD2( 0 ) );
}
}
/*
This code produces the following output:
5 is positive; use the sign "+".
-3 is negative; use the sign "-".
0 is zero; use the sign "".
*/
[C++]
#using <mscorlib.dll>
using namespace System;
// Declares a delegate for a method that takes in an int and returns a String.
__delegate String* myMethodDelegate(int myInt);
// Defines some methods to which the delegate can point.
__gc class mySampleClass {
public:
// Defines an instance method.
String* myStringMethod (int myInt) {
if (myInt > 0)
return(S"positive");
if (myInt < 0)
return(S"negative");
return (S"zero");
}
// Defines a static method.
static String* mySignMethod (int myInt) {
if (myInt > 0)
return(S"+");
if (myInt < 0)
return(S"-");
return (S"");
}
};
int main() {
// Creates one delegate for each method.
mySampleClass* mySC = new mySampleClass();
myMethodDelegate* myD1 = new myMethodDelegate(mySC, mySampleClass::myStringMethod);
myMethodDelegate* myD2 = new myMethodDelegate(0, mySampleClass::mySignMethod);
// Invokes the delegates.
Console::WriteLine(S"{0} is {1}; use the sign \"{2}\".", __box(5), myD1(5), myD2(5));
Console::WriteLine(S"{0} is {1}; use the sign \"{2}\".", __box(-3), myD1(-3), myD2(-3));
Console::WriteLine(S"{0} is {1}; use the sign \"{2}\".", __box(0), myD1(0), myD2(0));
}
/*
This code produces the following output:
5 is positive; use the sign "+".
-3 is negative; use the sign "-".
0 is zero; use the sign "".
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: Mscorlib (Mscorlib.dll 内)
参照
Delegate メンバ | System 名前空間 | MulticastDelegate | 非同期プログラミングの概要