System.Reflection.Emit.DynamicMethod.CreateDelegate Method

Completes the dynamic method and creates a delegate that can be used to execute it, specifying the delegate type and an object the delegate is bound to.

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public override sealed Delegate CreateDelegate (Type delegateType, object target)

Parameters

delegateType
A delegate type whose signature matches that of the dynamic method, minus the first parameter.
target
An object the delegate is bound to. Must be of the same type as the first parameter of the dynamic method.

Returns

A delegate of the specified type, which can be used to execute the dynamic method with the specified target object.

Remarks

This method overload creates a delegate bound to a particular object. Such a delegate is said to be closed over its first argument. Although the method is static, it acts as if it were an instance method; the instance is target.

This method overload requires target to be of the same type as the first parameter of the dynamic method, or to be assignable to that type (for example, a derived class). The signature of delegateType has all the parameters of the dynamic method except the first. For example, if the dynamic method has the parameters string, int, and byte, then delegateType has the parameters int and byte; target is of type string.

Calling the DynamicMethod.CreateDelegate(Type) method or the erload:System.Reflection.Emit.DynamicMethod.Invoke method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown.

To create a method body for a dynamic method when you have your own MSIL generator, call the DynamicMethod.GetDynamicILInfo method to obtain a System.Reflection.Emit.DynamicILInfo object. If you do not have your own MSIL generator, call the erload:System.Reflection.Emit.DynamicMethod.GetILGenerator method to obtain an System.Reflection.Emit.ILGenerator object that can be used to generate the method body.

Requirements

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0