Export (0) Print
Expand All

DynamicMethod.CreateDelegate Method (Type)

Completes the dynamic method and creates a delegate that can be used to execute it.

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

<ComVisibleAttribute(True)> _
Public Overrides NotOverridable Function CreateDelegate ( _
	delegateType As Type _
) As Delegate


Type: System.Type

A delegate type whose signature matches that of the dynamic method.

Return Value

Type: System.Delegate
A delegate of the specified type, which can be used to execute the dynamic method.


The dynamic method has no method body.


delegateType has the wrong number of parameters or the wrong parameter types.

Calling the CreateDelegate method or the 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 GetDynamicILInfo method to obtain a DynamicILInfo object. If you do not have your own MSIL generator, call the GetILGenerator method to obtain an ILGenerator object that can be used to generate the method body.

The following code example creates a dynamic method that takes two parameters. The example emits a simple function body that prints the first parameter to the console, and the example uses the second parameter as the return value of the method. The example completes the method by creating a delegate, invokes the delegate with different parameters, and finally invokes the dynamic method using the Invoke method.

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

Public Class Test
    ' Declare a delegate that will be used to execute the completed 
    ' dynamic method.  
    Private Delegate Function HelloInvoker(ByVal msg As String, _
        ByVal ret As Integer) As Integer 

    Public Shared Sub Main()
        ' Create an array that specifies the types of the parameters 
        ' of the dynamic method. This method has a String parameter 
        ' and an Integer parameter. 
        Dim helloArgs() As Type = {GetType(String), GetType(Integer)}

        ' Create a dynamic method with the name "Hello", a return type 
        ' of Integer, and two parameters whose types are specified by 
        ' the array helloArgs. Create the method in the module that 
        ' defines the Test class. 
        Dim hello As New DynamicMethod("Hello", _
            GetType(Integer), _
            helloArgs, _

        ' Create an array that specifies the parameter types of the 
        ' overload of Console.WriteLine to be used in Hello. 
        Dim writeStringArgs() As Type = {GetType(String)}
        ' Get the overload of Console.WriteLine that has one 
        ' String parameter. 
        Dim writeString As MethodInfo = GetType(Console). _
            GetMethod("WriteLine", writeStringArgs) 

        ' Get an ILGenerator and emit a body for the dynamic method. 
        Dim il As ILGenerator = hello.GetILGenerator()
        ' Load the first argument, which is a string, onto the stack.
        ' Call the overload of Console.WriteLine that prints a string.
        il.EmitCall(OpCodes.Call, writeString, Nothing)
        ' The Hello method returns the value of the second argument; 
        ' to do this, load the onto the stack and return.

        ' Create a delegate that represents the dynamic method. This 
        ' action completes the method, and any further attempts to 
        ' change the method will cause an exception. 
	Dim hi As HelloInvoker = _

        ' Use the delegate to execute the dynamic method. Save and 
        ' print the return value. 
        Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42)
        Console.WriteLine("Executing delegate hi(""Hello, World!"", 42) returned " _
            & retval)

        ' Do it again, with different arguments.
        retval = hi(vbCrLf & "Hi, Mom!", 5280)
        Console.WriteLine("Executing delegate hi(""Hi, Mom!"", 5280) returned " _
            & retval)

        ' Create an array of arguments to use with the Invoke method. 
        Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42}
        ' Invoke the dynamic method using the arguments. This is much 
        ' slower than using the delegate, because you must create an 
        ' array to contain the arguments, and ValueType arguments 
        ' must be boxed. Note that this overload of Invoke is  
        ' inherited from MethodBase, and simply calls the more  
        ' complete overload of Invoke. 
        Dim objRet As Object = hello.Invoke(Nothing, invokeArgs)
        Console.WriteLine("hello.Invoke returned " & objRet)
    End Sub 
End Class 

' This code example produces the following output: 

'Hello, World! 
'Executing delegate hi("Hello, World!", 42) returned 42 

'Hi, Mom! 
'Executing delegate hi("Hi, Mom!", 5280) returned 5280 

'Hello, World! 
'hello.Invoke returned 42 

.NET Framework

Supported in: 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 Silverlight 8.1, Windows Phone Silverlight 8

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.

© 2014 Microsoft