DynamicMethod Constructor (String, Type, array<Type>[]()[])

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Creates an anonymously hosted dynamic method, specifying the method name, return type, and parameter types.

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

Syntax

Public Sub New ( _
    name As String, _
    returnType As Type, _
    parameterTypes As Type() _
)
public DynamicMethod(
    string name,
    Type returnType,
    Type[] parameterTypes
)

Parameters

  • name
    Type: System..::.String
    The name of the dynamic method. This can be a zero-length string, but it cannot be nullNothingnullptra null reference (Nothing in Visual Basic).
  • returnType
    Type: System..::.Type
    A Type object that specifies the return type of the dynamic method, or nullNothingnullptra null reference (Nothing in Visual Basic) if the method has no return type.
  • parameterTypes
    Type: array<System..::.Type>[]()[]
    An array of Type objects specifying the types of the parameters of the dynamic method, or nullNothingnullptra null reference (Nothing in Visual Basic) if the method has no parameters.

Exceptions

Exception Condition
ArgumentException

An element of parameterTypes is nullNothingnullptra null reference (Nothing in Visual Basic) or Void.

ArgumentNullException

name is nullNothingnullptra null reference (Nothing in Visual Basic).

NotSupportedException

returnType is a type for which Type..::.IsByRef returns true.

Remarks

The dynamic method that is created by this constructor is associated with an anonymous assembly instead of an existing type or module. In Windows Phone applications, dynamic methods cannot be associated with existing types or modules. The anonymous assembly exists only to provide a sandbox environment for dynamic methods, that is, to isolate them from other code.

Just-in-time (JIT) visibility checks are enforced for the Microsoft intermediate language (MSIL) of the dynamic method. That is, the code in the dynamic method has access to public methods of public classes. Exceptions are thrown if the method tries to access types or members that are private, protected, or internal (Friend in Visual Basic).

This constructor specifies the method attributes MethodAttributes..::.Public and MethodAttributes..::.Static, and the calling convention CallingConventions..::.Standard.

Examples

The following example demonstrates the use of this constructor to emit and execute a simple dynamic method. The method has two parameters, a format string and an integer. The method calls the String..::.Format(String, Object) method overload with these two arguments, and returns the result.

Imports System.Reflection.Emit
Imports System.Reflection

Public Class Example

   ' Declare a delegate that can be used to execute the dynamic method.
   Private Delegate Function Caller(ByVal msg As String, _
        ByVal number As Integer) As String

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' 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 paramTypes() As Type = { GetType(String), GetType(Integer) }

      ' Create an unnamed dynamic method with a return type of
      ' Integer and with two parameters whose types are specified by
      ' the array paramTypes. The dynamic method is anonymously
      ' hosted. 
      Dim method As New DynamicMethod("", GetType(String), paramTypes)

      ' Get a MethodInfo for the overload of String.Format that
      ' takes a format string and an object for insertion in the format 
      ' string. The dynamic method uses this overload to format the 
      ' return string.
      Dim stringFormat As MethodInfo = GetType(String). _
          GetMethod("Format", New Type() { GetType(String), GetType(Object) })

      ' Get an ILGenerator and emit a body for the dynamic method.
      Dim il As ILGenerator = method.GetILGenerator()

      ' Load the arguments onto the execution stack. The second 
      ' argument is an Integer, so it must be boxed before it can be 
      ' passed to a parameter of type Object. 
      il.Emit(OpCodes.Ldarg_0)
      il.Emit(OpCodes.Ldarg_1)
      il.Emit(OpCodes.Box, GetType(Integer))

      ' Call the String.Format method. The return value from that call 
      ' is placed on the execution stack, so the dynamic method simply 
      ' returns.         
      il.Emit(OpCodes.Call, stringFormat)
      il.Emit(OpCodes.Ret)

      ' Create a delegate that represents the dynamic method. This
      ' action completes the dynamic method, and any further attempts
      ' to change the method have no effect.
      Dim callMethod As Caller = method.CreateDelegate(GetType(Caller))

      ' Invoke the delegate and display the result.
      Dim ret As String = callMethod("The second argument is: {0}.", 1969)

      outputBlock.Text &= _
         String.Format("Invoking the delegate returned the string '{0}'.", ret)

   End Sub

End Class

' This code example produces the following output:
'
'Invoking the delegate returned the string 'The second argument is: 1969.'.
using System.Reflection.Emit;
using System.Reflection;
using System;

public class Example
{
   // Declare a delegate that can be used to execute the dynamic method.
   private delegate string Caller(string msg, int number);

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an array that specifies the types of the parameters
      // of the dynamic method. This method has a String parameter
      // and an Integer parameter.
      Type[] paramTypes = { typeof(string), typeof(int) };

      // Create an unnamed dynamic method with a return type of
      // Integer and with two parameters whose types are specified by
      // the array paramTypes. The dynamic method is anonymously
      // hosted. 
      DynamicMethod method = new DynamicMethod("", typeof(string), paramTypes);

      // Get a MethodInfo for the overload of String.Format that
      // takes a format string and an object for insertion in the format 
      // string. The dynamic method uses this overload to format the 
      // return string.
      MethodInfo stringFormat = typeof(string).GetMethod("Format", 
         new Type[] { typeof(string), typeof(object) });

      // Get an ILGenerator and emit a body for the dynamic method.
      ILGenerator il = method.GetILGenerator();

      // Load the arguments onto the execution stack. The second 
      // argument is an Integer, so it must be boxed before it can be 
      // passed to a parameter of type Object. 
      il.Emit(OpCodes.Ldarg_0);
      il.Emit(OpCodes.Ldarg_1);
      il.Emit(OpCodes.Box, typeof(int));

      // Call the String.Format method. The return value from that call 
      // is placed on the execution stack, so the dynamic method simply 
      // returns.         
      il.Emit(OpCodes.Call, stringFormat);
      il.Emit(OpCodes.Ret);

      // Create a delegate that represents the dynamic method. This
      // action completes the dynamic method, and any further attempts
      // to change the method have no effect.
      Caller callMethod = (Caller) method.CreateDelegate(typeof(Caller));

      // Invoke the delegate and display the result.
      string ret = callMethod("The second argument is: {0}.", 1969);

      outputBlock.Text += 
         String.Format("Invoking the delegate returned the string '{0}'.", ret);
   }
}

/* This code example produces the following output:

Invoking the delegate returned the string 'The second argument is: 1969.'.
 */

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Platforms

Windows Phone

See Also

Reference

DynamicMethod Class

DynamicMethod Overload

System.Reflection.Emit Namespace