Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Delegate.CreateDelegate Method (Type, MethodInfo, Boolean)

Creates a delegate of the specified type to represent the specified static method, with the specified behavior on failure to bind.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static Delegate CreateDelegate(
	Type type,
	MethodInfo method,
	bool throwOnBindFailure
)

Parameters

type
Type: System.Type

The Type of delegate to create.

method
Type: System.Reflection.MethodInfo

The MethodInfo describing the static or instance method the delegate is to represent.

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 to represent the specified static method.
ExceptionCondition
ArgumentNullException

type is null.

-or-

method is null.

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 the permissions necessary to access method.

This method overload can create open static method delegates and open instance method delegates — that is, delegates that expose the hidden first argument of instance methods. For a detailed explanation, see the more general CreateDelegate(Type, Object, MethodInfo, Boolean) method overload, which allows you to create all combinations of open or closed delegates for instance or static methods.

NoteNote

This method overload should be used when the delegate is not closed over its first argument, because it is somewhat faster in that case.

NoteNote

Starting with the .NET Framework 2.0 Service Pack 1, this method can be used to access non-public methods if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public methods is restricted to the caller’s grant set, or a subset thereof. (See Security Considerations for Reflection.)

To use this functionality, your application should target the .NET Framework 3.5 or later.

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.

NoteNote

In the .NET Framework version 1.0 and 1.1, the types must 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, a delegate with a parameter of type Hashtable and a return type of Object can represent a method with a parameter of type Object and a return value of type Hashtable.

This section contains two code examples. The first example demonstrates the two kinds of delegates that can be created with this method overload: open over an instance method and open over a static method.

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

Example 1

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

NoteNote

There are two overloads of the CreateDelegate method that specify a MethodInfo but not a first argument; 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 two delegate types: D1 takes an instance of C and a string, and D2 takes a string.

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

  • 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.

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) 
    { 
        Console.WriteLine("Instance method M1 on C:  id = {0}, s = {1}",
            this.id, s);
    }

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

public class Example
{
    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!
 */

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.

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 Example(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 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).  
        //
        Example ex = 
            (Example) Delegate.CreateDelegate(typeof(Example), minfo);

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.