Partager via


Delegate.CreateDelegate Method (Type, Object, MethodInfo, Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a delegate of the specified type that represents the specified static or instance method, with the specified first argument and the specified behavior on failure to bind.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function CreateDelegate ( _
    type As Type, _
    firstArgument As Object, _
    method As MethodInfo, _
    throwOnBindFailure As Boolean _
) As Delegate
[SecuritySafeCriticalAttribute]
public static Delegate CreateDelegate(
    Type type,
    Object firstArgument,
    MethodInfo method,
    bool throwOnBindFailure
)

Parameters

  • type
    Type: System.Type
    A type representing the type of delegate to create.
  • firstArgument
    Type: System.Object
    The first argument of the method the delegate represents. For instance methods, it must be compatible with the instance type.
  • throwOnBindFailure
    Type: System.Boolean
    true to throw an exception if method cannot be bound; otherwise, false.

Return Value

Type: System.Delegate
A delegate of the specified type that represents the specified static or instance method, or nulla null reference (Nothing in Visual Basic) if throwOnBindFailure is false and the delegate cannot be bound to method.

Exceptions

Exception Condition
ArgumentNullException

type is nulla null reference (Nothing in Visual Basic).

-or-

method is nulla null reference (Nothing in Visual Basic).

ArgumentException

type does not inherit MulticastDelegate.

-or-

type is not a RuntimeType. See Runtime Types in Reflection.

-or-

method cannot be bound, and throwOnBindFailure is true.

-or-

method is not a RuntimeMethodInfo. See Runtime Types in Reflection.

MissingMethodException

The Invoke method of type is not found.

MethodAccessException

The caller does not have access to method.

-or-

Application code attempts to access this member late-bound, for example, by using the Type.InvokeMember method.

Remarks

In Silverlight, method must specify an accessible method.

This method overload and the CreateDelegate(Type, Object, MethodInfo) method overload, which always throws on failure to bind, provide the most flexible way to create delegates. You can use them to create delegates for either static or instance methods, with or without a first argument.

NoteNote:

If you do not supply a first argument, use the CreateDelegate(Type, MethodInfo, Boolean) method overload for better performance.

The delegate type and the method must have compatible return types. That is, the return type of method must be assignable to the return type of type.

If firstArgument is supplied, it is passed to method every time the delegate is invoked; firstArgument is said to be bound to the delegate, and the delegate is said to be closed over its first argument. If method is static (Shared in Visual Basic), the argument list supplied when invoking the delegate includes all parameters except the first; if method is an instance method, then firstArgument is passed to the hidden instance parameter (represented by this in C#, or by Me in Visual Basic).

If firstArgument is supplied, the first parameter of method must be a reference type, and firstArgument must be compatible with that type.

Important noteImportant Note:

If method is static (Shared in Visual Basic) and its first parameter is of type Object or ValueType, then firstArgument can be a value type. In this case firstArgument is automatically boxed. Automatic boxing does not occur for any other arguments, as it would in a C# or Visual Basic function call.

If firstArgument is a null reference and method is an instance method, the result depends on the signatures of the delegate type type and of method:

  • If the signature of type explicitly includes the hidden first parameter of method, the delegate is said to represent an open instance method. When the delegate is invoked, the first argument in the argument list is passed to the hidden instance parameter of method.

  • If the signatures of method and type match (that is, all parameter types are compatible), then the delegate is said to be closed over a null reference. Invoking the delegate is like calling an instance method on a null instance, which is not a particularly useful thing to do.

If firstArgument is a null reference and method is static, the result depends on the signatures of the delegate type type and of method:

  • If the signature of method and type match (that is, all parameter types are compatible), the delegate is said to represent an open static method. This is the most common case for static methods. In this case, you can get slightly better performance by using the CreateDelegate(Type, MethodInfo, Boolean) method overload.

  • If the signature of type begins with the second parameter of method and the rest of the parameter types are compatible, then the delegate is said to be closed over a null reference. When the delegate is invoked, a null reference is passed to the first parameter of method.

Compatible Parameter Types and Return Type

The parameter types and return type of a delegate must be compatible with the parameter types and return type of the method the delegate represents; the types do not have to match exactly.

A parameter of a delegate is compatible with the corresponding parameter of a method if the type of the delegate parameter is more restrictive than the type of the method parameter, because this guarantees that an argument passed to the delegate can be passed safely to the method.

Similarly, the return type of a delegate is compatible with the return type of a method if the return type of the method is more restrictive than the return type of the delegate, because this guarantees that the return value of the method can be cast safely to the return type of the delegate.

For example, given two types Base and Derived, such that Derived inherits Base, a delegate with a parameter of type Derived and a return type of Base can represent a method with a parameter of type Base and a return value of type Derived.

Determining the Methods a Delegate Can Represent

Another useful way to think of the flexibility provided by this overload of CreateDelegate is that any given delegate can represent four different combinations of method signature and method kind (static versus instance). Consider a delegate type D with one argument of type C. The following describes the methods D can represent, ignoring the return type since it must match in all cases:

  • D can represent any instance method that has exactly one argument of type C, regardless of what type the instance method belongs to. When CreateDelegate is called, firstArgument is an instance of the type method belongs to, and the resulting delegate is said to be closed over that instance. (Trivially, D can also be closed over a null reference if firstArgument is nulla null reference (Nothing in Visual Basic).)

  • D can represent an instance method of C that has no arguments. When CreateDelegate is called, firstArgument is a null reference. The resulting delegate represents an open instance method, and an instance of C must be supplied each time it is invoked.

  • D can represent a static method that takes one argument of type C, and that method can belong to any type. When CreateDelegate is called, firstArgument is a null reference. The resulting delegate represents an open static method, and an instance of C must be supplied each time it is invoked.

  • D can represent a static method that belongs to type F and has two arguments, of type F and type C. When CreateDelegate is called, firstArgument is an instance of F. The resulting delegate represents a static method that is closed over that instance of F. Note that in the case where F and C are the same type, the static method has two arguments of that type. (In this case, D is closed over a null reference if firstArgument is nulla null reference (Nothing in Visual Basic).)

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 CreateDelegate throws MissingMethodException when attempting to create a delegate with incorrect arguments. Delegate.CreateDelegate throws TargetInvocationException when called on a null object. Delegate.CreateDelegate throws MissingMethodException instead of ArgumentException for non-instance methods.

Examples

This section contains three code examples. The first example demonstrates the four kinds of delegates that can be created: closed over an instance method, open over an instance method, open over a static method, and closed over a static method.

The second code example demonstrates compatible parameter types and return types.

The third code example defines a single delegate type, and shows all the methods that delegate type can represent.

Example 1

The following code example demonstrates the four ways a delegate can be created using this overload of the CreateDelegate method.

NoteNote:

There are two overloads of the CreateDelegate method that specify firstArgument and a MethodInfo; their functionality is the same except that one allows you to specify whether to throw on failure to bind, and the other always throws. This code example uses both overloads.

The example declares a class C with a static method M2 and an instance method M1, and three delegate types: D1 takes an instance of C and a string, D2 takes a string, and D3 has no arguments.

A second class named Example contains the code that creates the delegates.

  • A delegate of type D2, closed over an instance of C, is created for the instance method M1. It is invoked with different strings, to show that the bound instance of C is always used.

  • A delegate of type D1, representing an open instance method, is created for the instance method M1. An instance must be passed when the delegate is invoked.

  • A delegate of type D2, representing an open static method, is created for the static method M2.

  • Finally, a delegate of type D3, closed over a string, is created for the static method M2. The method is invoked to show that it uses the bound string.

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 C, 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 C
   Private id As Integer
   Public Sub New(ByVal id As Integer)
      Me.id = id
   End Sub 'New

   Public Sub M1(ByVal s As String)
      outputBlock.Text &= String.Format("Instance method M1 on C:  id = {0}, s = {1}", _
          Me.id, s) & vbCrLf
   End Sub

   Public Shared Sub M2(ByVal s As String)
      outputBlock.Text &= String.Format("Shared method M2 on C:  s = {0}", s) & vbCrLf
   End Sub

   ' Provide access to the TextBlock for output.
   Friend Shared outputBlock As System.Windows.Controls.TextBlock
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Initialize class C for output to the TextBlock.
      C.outputBlock = outputBlock

      Dim c1 As New C(42)

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

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


      outputBlock.Text &= vbLf & "An instance method closed over C." & vbCrLf
      ' 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 C).
      '
      If test IsNot Nothing Then
         d2 = CType(test, D2)

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


      outputBlock.Text &= vbLf & "An open instance method." & vbCrLf
      ' 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 C must be passed in each time the 
      ' delegate is invoked.
      '
      d1(c1, "Hello, World!")
      d1(New C(5280), "Hi, Mom!")


      outputBlock.Text &= vbLf & "An open Shared method." & vbCrLf
      ' 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 C are involved, because this is a Shared
      ' method. 
      '
      d2("Hello, World!")
      d2("Hi, Mom!")


      outputBlock.Text &= vbLf & "A Shared method closed over the first argument (String)." & vbCrLf
      ' 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 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 Shared method.
'Shared method M2 on C:  s = Hello, World!
'Shared method M2 on C:  s = Hi, Mom!
'
'A Shared method closed over the first argument (String).
'Shared method M2 on C:  s = Hello, World!
' 
using System;
using System.Reflection;
using System.Security.Permissions;

// 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)
   {
      outputBlock.Text += String.Format("Instance method M1 on C:  id = {0}, s = {1}",
          this.id, s) + "\n";
   }

   public static void M2(string s)
   {
      outputBlock.Text += String.Format("Static method M2 on C:  s = {0}", s) + "\n";
   }

   internal static System.Windows.Controls.TextBlock outputBlock;
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Initialize class C for output to the TextBlock control.
      C.outputBlock = outputBlock;

      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;


      outputBlock.Text += "\nAn instance method closed over C." + "\n";
      // 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!");
      }


      outputBlock.Text += "\nAn open instance method." + "\n";
      // 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!");


      outputBlock.Text += "\nAn open static method." + "\n";
      // 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!");


      outputBlock.Text += "\nA static method closed over the first argument (String)." + "\n";
      // 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!
 */

Example 2

The following code example demonstrates compatibility of parameter types and return types.

NoteNote:

This code example uses the CreateDelegate(Type, MethodInfo) method overload. The use of other overloads that take MethodInfo is similar.

The code example defines a base class named Base and a class named Derived that derives from Base. The derived class has a static (Shared in Visual Basic) method named MyMethod with one parameter of type Base and a return type of Derived. The code example also defines a delegate named Example that has one parameter of type Derived and a return type of Base.

The code example demonstrates that the delegate named Example can be used to represent the method MyMethod. The method can be bound to the delegate because:

  • The parameter type of the delegate (Derived) is more restrictive than the parameter type of MyMethod (Base), so that it is always safe to pass the argument of the delegate to MyMethod.

  • The return type of MyMethod (Derived) is more restrictive than the parameter type of the delegate (Base), so that it is always safe to cast the return type of the method to the return type of the delegate.

The code example produces no output.

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 Caller(ByVal arg As Derived) As Base

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' 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 covariance of parameter types and contravariance
      ' of return types by using the delegate Caller 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 Caller (that
      ' is, the method returns an instance of Derived, which can
      ' always be safely cast to type Base). 
      '
      Dim ex As Caller = CType( _
          [Delegate].CreateDelegate(GetType(Caller), minfo),  _
          Caller _
      )

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

' This example produces no output.
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 Caller(Derived arg);

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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 covariance of parameter types and contravariance
      // of return types by using the delegate Caller 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 Caller (that
      // is, the method returns an instance of Derived, which can
      // always be safely cast to type Base). 
      //
      Caller ex =
          (Caller)Delegate.CreateDelegate(typeof(Caller), minfo);

      // Execute MyMethod using the delegate Caller.
      //        
      Base b = ex(new Derived());
   }
}

// This code example produces no output.

Example 3

The following code example shows all the methods a single delegate type can represent.

NoteNote:

There are two overloads of the CreateDelegate method that specify firstArgument and a MethodInfo; their functionality is the same except that one allows you to specify whether to throw on failure to bind, and the other always throws. This code example uses both overloads.

The code example defines two classes, C and F, and a delegate type D with one argument of type C. The classes have matching static and instance methods M1, M3, and M4, and class C also has an instance method M2 that has no arguments.

A third class named Example contains the code that creates the delegates.

  • Delegates are created for instance method M1 of type C and type F; each is closed over an instance of the respective type. Method M1 of type C displays the ID properties of the bound instance and of the argument.

  • A delegate is created for method M2 of type C. This is an open instance delegate, in which the argument of the delegate represents the hidden first argument on the instance method. The method has no other arguments.

  • Delegates are created for static method M3 of type C and type F; these are open static delegates.

  • Finally, delegates are created for static method M4 of type C and type F; each method has the declaring type as its first argument, and an instance of the type is supplied, so the delegates are closed over their first arguments. Method M4 of type C displays the ID properties of the bound instance and of the argument.

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)
      outputBlock.Text &= _
         String.Format("Instance method M1(c As C) on C:  this.id = {0}, c.ID = {1}" & vbCrLf, _
                       Me.ID, c.ID)
   End Sub

   Public Sub M2()
      outputBlock.Text &= _
         String.Format("Instance method M2() on C:  this.id = {0}", Me.ID) & vbCrLf
   End Sub

   Public Shared Sub M3(ByVal c As C)
      outputBlock.Text &= _
         String.Format("Shared method M3(c As C) on C:  c.ID = {0}", c.ID) & vbCrLf
   End Sub

   Public Shared Sub M4(ByVal c1 As C, ByVal c2 As C)
      outputBlock.Text &= _
         String.Format("Shared method M4(c1 As C, c2 As C) on C:  c1.ID = {0}, c2.ID = {1}" & vbCrLf, _
                       c1.ID, c2.ID)
   End Sub

   ' Provide access to the TextBlock for output.
   Friend Shared outputBlock As System.Windows.Controls.TextBlock
End Class


Public Class F

   Public Sub M1(ByVal c As C)
      outputBlock.Text &= _
         String.Format("Instance method M1(c As C) on F:  c.ID = {0}", c.ID) & vbCrLf
   End Sub

   Public Shared Sub M3(ByVal c As C)
      outputBlock.Text &= _
         String.Format("Shared method M3(c As C) on F:  c.ID = {0}", c.ID) & vbCrLf
   End Sub

   Public Shared Sub M4(ByVal f As F, ByVal c As C)
      outputBlock.Text &= _
         String.Format("Shared method M4(f As F, c As C) on F:  c.ID = {0}", c.ID) & vbCrLf
   End Sub

   ' Provide access to the TextBlock for output.
   Friend Shared outputBlock As System.Windows.Controls.TextBlock
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Initialize the test classes for output to the TextBlock.
      C.outputBlock = outputBlock
      F.outputBlock = outputBlock

      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")

      outputBlock.Text &= String.Format(vbLf & "An instance method on any type, with an argument of type C.") & vbCrLf
      ' 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)

      outputBlock.Text &= vbLf & "An instance method on C with no arguments." & vbCrLf
      ' 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)

      outputBlock.Text &= String.Format(vbLf & "A Shared method on any type, with an argument of type C.") & vbCrLf
      ' 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)

      outputBlock.Text &= String.Format(vbLf & "A Shared method on any type, with an argument of") & vbCrLf
      outputBlock.Text &= "    that type and an argument of type C." & vbCrLf
      ' 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
'
using System;
using System.Reflection;
using 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 void D(C c);

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

   public void M1(C c)
   {
      outputBlock.Text += String.Format("Instance method M1(C c) on C:  this.id = {0}, c.ID = {1}\n",
          this.id, c.ID);
   }

   public void M2()
   {
      outputBlock.Text += String.Format("Instance method M2() on C:  this.id = {0}\n",
          this.id);
   }

   public static void M3(C c)
   {
      outputBlock.Text += String.Format("Static method M3(C c) on C:  c.ID = {0}\n", c.ID);
   }

   public static void M4(C c1, C c2)
   {
      outputBlock.Text += String.Format("Static method M4(C c1, C c2) on C:  c1.ID = {0}, c2.ID = {1}\n",
          c1.ID, c2.ID);
   }

   // Provide access to the TextBlock for output.
   internal static System.Windows.Controls.TextBlock outputBlock; 
}

public class F
{
   public void M1(C c)
   {
      outputBlock.Text += String.Format("Instance method M1(C c) on F:  c.ID = {0}\n",
          c.ID);
   }

   public static void M3(C c)
   {
      outputBlock.Text += String.Format("Static method M3(C c) on F:  c.ID = {0}\n", c.ID);
   }

   public static void M4(F f, C c)
   {
      outputBlock.Text += String.Format("Static method M4(F f, C c) on F:  c.ID = {0}\n",
          c.ID);
   }

   // Provide access to the TextBlock for output.
   internal static System.Windows.Controls.TextBlock outputBlock; 
}


public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Initialize the test classes for output to the TextBlock.
      C.outputBlock = outputBlock;
      F.outputBlock = outputBlock;

      C c1 = new C(42);
      C c2 = new C(1491);
      F f1 = new F();

      D d;

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

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

      outputBlock.Text += "\nAn instance method on any type, with an argument of type C.\n";
      // D can represent any instance method that exactly matches its
      // signature. Methods on C 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);

      outputBlock.Text += "\nAn instance method on C with no arguments.\n";
      // 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.
      //
      d = (D)Delegate.CreateDelegate(typeof(D), null, cmi2);
      d(c1);

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

      outputBlock.Text += "\nA static method on any type, with an argument of\n";
      outputBlock.Text += "    that type and an argument of type C." + "\n";
      // 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.
      //
      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 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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.