System 名前空間


.NET Framework クラス ライブラリ
Delegate クラス

更新 : 2007 年 11 月

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

名前空間 :  System
アセンブリ :  mscorlib (mscorlib.dll 内)
構文

Visual Basic (宣言)
<SerializableAttribute> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Delegate _
    Implements ICloneable, ISerializable
Visual Basic (使用法)
Dim instance As [Delegate]
C#
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDual)]
[ComVisibleAttribute(true)]
public abstract class Delegate : ICloneable, 
    ISerializable
Visual C++
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType::AutoDual)]
[ComVisibleAttribute(true)]
public ref class Delegate abstract : ICloneable, 
    ISerializable
J#
/** @attribute SerializableAttribute */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDual) */
/** @attribute ComVisibleAttribute(true) */
public abstract class Delegate implements ICloneable, 
    ISerializable
JScript
public abstract class Delegate implements ICloneable, ISerializable
解説

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

ほとんどの言語では delegate キーワードが実装されています。また、それらの言語のコンパイラは MulticastDelegate クラスから派生させることができます。このため、開発者は各言語で提供されている delegate キーワードを使用する必要があります。

共通言語ランタイムには、継承されたメソッドに加えて、BeginInvoke および EndInvoke というデリゲート型用の 2 つの特別なメソッドが用意されています。これらのメソッドの詳細については、「同期メソッドの非同期呼び出し」を参照してください。

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

  • 型のインスタンス メソッドと、その型に適用できるターゲット オブジェクト。

  • 仮パラメータ リストに非表示の this パラメータを指定した、型のインスタンス メソッド。デリゲートは、オープンなインスタンス デリゲートであると言います。

  • 静的メソッド。

  • 静的メソッドと、そのメソッドの最初のパラメータに適用できるターゲット オブジェクト。デリゲートは、その最初の引数に対して閉じられていると言います。

デリゲート バインディングの詳細については、「共通型システムのデリゲート」と「CreateDelegate(Type, Object, MethodInfo, Boolean)」を参照してください。

y22acf51.alert_note(ja-jp,VS.90).gifメモ :

.NET Framework Version 1.0 および Version 1.1 では、メソッドのシグネチャがデリゲート型で指定されたシグネチャと完全に一致する場合にのみ、デリゲートはメソッドを表すことができます。つまり、上述の最初の項目と 3 番目の項目のみがサポートされ、最初の項目では完全な型の一致が必要です。

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

デリゲートが静的メソッドを表している場合、デリゲートはそのメソッドのエントリ ポイントへの参照を格納しています。デリゲートが最初の引数に対して閉じている静的メソッドを表している場合、デリゲートはメソッドのエントリ ポイントへの参照と、そのメソッドの最初の引数の型に適用できるターゲット オブジェクトへの参照を格納しています。デリゲートを呼び出したときに、最初の静的メソッドの引数はターゲット オブジェクトを受け取ります。

デリゲートの呼び出しリストは、順序付けられたデリゲートのセットであり、呼び出しリスト内の各要素はデリゲートによって呼び出されるメソッドを 1 つずつ表します。呼び出しリストには、重複メソッドを格納できます。デリゲートがメソッドを呼び出すときは、呼び出しリストに出現する順にメソッドを呼び出します。デリゲートは呼び出しリスト内のすべてのメソッドを呼び出そうとします。重複メソッドは、呼び出しリストに出現するたびに呼び出されます。デリゲートは不変であるため、デリゲートの呼び出しリストを一度作成すると、そのリストは変わりません。

複数のメソッドを呼び出したり、組み合わせ演算で使用できるデリゲートを、それぞれ "マルチキャスト デリゲート" および "組み合わせ可能デリゲート" と呼びます。

CombineRemove などの組み合わせ演算によって既存のデリゲートが変更されることはありません。その代わり、組み合わせ演算では、演算の結果、または変更されないデリゲート、または nullNothingnullptrnull 参照 (Visual Basic では Nothing) が格納された新しいデリゲートが返されます。演算の結果がメソッドをまったく参照していないデリゲートの場合は、nullNothingnullptrnull 参照 (Visual Basic では Nothing) が返されます。要求した演算が無効の場合は、変更されないデリゲートが返されます。

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

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

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


次の例では、myMethodDelegate という名前のデリゲートを定義する方法を示します。このデリゲートのインスタンスは、入れ子になっている mySampleClass クラスのインスタンス メソッドおよび静的メソッドに対応して作成されています。インスタンス メソッドに対応するデリゲートには、mySampleClass のインスタンスが必要です。mySampleClass インスタンスは、mySC という名前の変数に保存されます。

Visual Basic
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. For the instance method, an
      ' instance (mySC) must be supplied. For the Shared method, the
      ' method name is qualified by the class name.
      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. For the instance method, an
      // instance (mySC) must be supplied. For the static method, use the
      // class name.
      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 "".
*/ 

Visual C++
using namespace System;
delegate String^ myMethodDelegate( // Declares a delegate for a method that takes in an int and returns a String.
int myInt );

// Defines some methods to which the delegate can point.
ref class mySampleClass
{
public:

   // Defines an instance method.
   String^ myStringMethod( int myInt )
   {
      if ( myInt > 0 )
            return ("positive");

      if ( myInt < 0 )
            return ("negative");

      return ("zero");
   }


   // Defines a static method.
   static String^ mySignMethod( int myInt )
   {
      if ( myInt > 0 )
            return ("+");

      if ( myInt < 0 )
            return ("-");

      return ("");
   }

};

int main()
{

   // Creates one delegate for each method. For the instance method, an 
   // instance (mySC) must be supplied. For the static method, only the
   // method name is needed.
   mySampleClass^ mySC = gcnew mySampleClass;
   myMethodDelegate^ myD1 = gcnew myMethodDelegate( mySC, &mySampleClass::myStringMethod );
   myMethodDelegate^ myD2 = gcnew 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 "".
*/
J#
import System.*;

public class SamplesDelegate
{
    /** @delegate 
     */
    // 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 static class MySampleClass
    {
        // Defines an instance method.
        public String MyStringMethod(int myInt)
        {
            if (myInt > 0)  {
                return "positive";
            }
            if (myInt < 0) {
                return "negative";
            }
            return "zero";
        } //myStringMethod

        // Defines a static method.
        public static String MySignMethod(int myInt)
        {
            if (myInt > 0) {
                return "+";
            }
            if (myInt < 0) {
                return "-";
            }
            return "";
        } //MySignMethod
    } //MySampleClass

    public static void main(String[] args)
    {
        // Creates one delegate for each method. For the instance method,
        // an instance (mySC) must be supplied. For the static method,
        // only the class name is required.
        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}\".", 
            (Int32)(5), System.Convert.ToString(myD1.Invoke(5)), 
            System.Convert.ToString(myD2.Invoke(5)));
        Console.WriteLine("{0} is {1}; use the sign \"{2}\".",
            System.Convert.ToString(-3), System.Convert.ToString(myD1.Invoke(-3)),
            System.Convert.ToString(myD2.Invoke(-3)));
        Console.WriteLine("{0} is {1}; use the sign \"{2}\".", (Int32)(0),
            System.Convert.ToString(myD1.Invoke(0)), System.Convert.ToString(
            myD2.Invoke(0)));
    } //main
} //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 "".
*/

継承階層

System..::.Object
  System..::.Delegate
    System..::.MulticastDelegate
スレッド セーフ

この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォーム

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
バージョン情報

.NET Framework

サポート対象 : 3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 3.5、2.0、1.0

XNA Framework

サポート対象 : 2.0、1.0
参照

参照

その他の技術情報

タグ :


Page view tracker