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.
- type
- A Type representing the type of delegate to create.
- firstArgument
- An object that is the first argument of the method the delegate represents. For instance methods, it must be compatible with the instance type.
- method
- The System.Reflection.MethodInfo describing the static or instance method the delegate is to represent.
- throwOnBindFailure
- true to throw an exception if method cannot be bound; otherwise, false.
A delegate of the specified type that represents the specified static or instance method, or null if throwOnBindFailure is false and the delegate cannot be bound to method.
This method overload and the Delegate.CreateDelegate(Type, object, System.Reflection.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.
If you do not supply a first argument, use the Delegate.CreateDelegate(Type, System.Reflection.MethodInfo, bool) 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.
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 Delegate.CreateDelegate(Type, System.Reflection.MethodInfo, bool) 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.
Starting with the net_v20sp1_long, this method can be used to access non-public methods if the caller has been granted System.Security.Permissions.ReflectionPermission with the System.Security.Permissions.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_v35_long or later.
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.
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.
Another useful way to think of the flexibility provided by this overload of erload:System.Delegate.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 Delegate.CreateDelegate(Type, object, System.Reflection.MethodInfo, bool) 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 null.)
D can represent an instance method of C that has no arguments. When Delegate.CreateDelegate(Type, object, System.Reflection.MethodInfo, bool) 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 Delegate.CreateDelegate(Type, object, System.Reflection.MethodInfo, bool) 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 Delegate.CreateDelegate(Type, object, System.Reflection.MethodInfo, bool) 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 null.)