DynamicMethod Constructor (String, Type, Type[], Module)


Creates a dynamic method that is global to a module, specifying the method name, return type, parameter types, and module.

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

public DynamicMethod(
	string name,
	Type returnType,
	Type[] parameterTypes,
	Module m


Type: System.String

The name of the dynamic method. This can be a zero-length string, but it cannot be null.

Type: System.Type

A Type object that specifies the return type of the dynamic method, or null if the method has no return type.

Type: System.Type[]

An array of Type objects specifying the types of the parameters of the dynamic method, or null if the method has no parameters.

Type: System.Reflection.Module

A Module representing the module with which the dynamic method is to be logically associated.

Exception Condition

An element of parameterTypes is null or Void.


m is a module that provides anonymous hosting for dynamic methods.


name is null.


m is null.


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

This constructor specifies method attributes MethodAttributes.Public and MethodAttributes.Static, calling convention CallingConventions.Standard, and does not skip just-in-time (JIT) visibility checks.

The dynamic method created with this constructor has access to public and internal (Friend in Visual Basic) members of all the types contained in module m.


For backward compatibility, this constructor demands SecurityPermission with the SecurityPermissionFlag.ControlEvidence flag if the following conditions are both true: m is a module other than the calling module, and the demand for ReflectionPermission with the ReflectionPermissionFlag.MemberAccess flag has failed. If the demand for SecurityPermission succeeds, the operation is allowed.


Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

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(Object, BindingFlags, Binder, Object[], CultureInfo) method.

using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;

public class Test
    // Declare a delegate that will be used to execute the completed
    // dynamic method. 
    private delegate int HelloInvoker(string msg, int ret);

    public static void Main()
        // Create an array that specifies the types of the parameters
        // of the dynamic method. This method has a string parameter
        // and an int parameter.
        Type[] helloArgs = {typeof(string), typeof(int)};

        // Create a dynamic method with the name "Hello", a return type
        // of int, and two parameters whose types are specified by the
        // array helloArgs. Create the method in the module that
        // defines the Test class.
        DynamicMethod hello = new DynamicMethod("Hello", 

        // Create an array that specifies the parameter types of the
        // overload of Console.WriteLine to be used in Hello.
        Type[] writeStringArgs = {typeof(string)};
        // Get the overload of Console.WriteLine that has one
        // String parameter.
        MethodInfo writeString = 
            typeof(Console).GetMethod("WriteLine", writeStringArgs);

        // Get an ILGenerator and emit a body for the dynamic method.
        ILGenerator il = 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, null);
        // 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.
        HelloInvoker hi = 
            (HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker));

        // Use the delegate to execute the dynamic method. Save and
        // print the return value.
        int retval = hi("\r\nHello, World!", 42);
        Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}",

        // Do it again, with different arguments.
        retval = hi("\r\nHi, Mom!", 5280);
        Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}",

        // Create an array of arguments to use with the Invoke method.
        object[] invokeArgs = {"\r\nHello, 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.
        object objRet = hello.Invoke(null, invokeArgs);
        Console.WriteLine("hello.Invoke returned {0}", objRet);


to associate the dynamic method with a module other than the calling module. Associated enumeration: ReflectionPermissionFlag.RestrictedMemberAccess.

.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.1
Return to top