Delegate.CreateDelegate Method (Type, MethodInfo)
Creates a delegate of the specified type to represent the specified static method.
Assembly: mscorlib (in mscorlib.dll)
Return ValueType: System.Delegate
A delegate of the specified type to represent the specified static method.
type is null.
method is null.
The Invoke method of type is not found.
The caller does not have the permissions necessary to access method.
In the .NET Framework version 1.0 and 1.1, this method overload creates delegates for static methods only. In the .NET Framework version 2.0, this method overload also can create open instance method delegates; that is, delegates that explicitly supply the hidden first argument of instance methods. For a detailed explanation, see the more general CreateDelegate(Type, Object, MethodInfo) method overload, which allows you to create all combinations of open or closed delegates for instance or static methods, and optionally to specify a first argument.
This method overload should be used when the delegate is not closed over its first argument, because it is somewhat faster in that case.
This method overload is equivalent to calling the CreateDelegate(Type, MethodInfo, Boolean) method overload and specifying true for throwOnBindFailure.
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.
In the .NET Framework version 2.0, the parameter types and return type of a delegate created using this method overload must be compatible with the parameter types and return type of the method the delegate represents; the types do not have to match exactly. This represents a relaxation of the binding behavior in the .NET Framework version 1.0 and 1.1, where 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.
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.
The following code example demonstrates the two ways a delegate can be created using this overload of the CreateDelegate method.
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.
The following code example demonstrates compatibility of parameter types and return types.
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.
for accessing a non-public method when the grant set of the non-public method is restricted to the caller's grant set, or a subset thereof. Associated enumeration: ReflectionPermissionFlag.RestrictedMemberAccess
for accessing a non-public method regardless of its grant set. Associated enumeration: ReflectionPermissionFlag.MemberAccess
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0