This documentation is archived and is not being maintained.

DynamicMethod Class

Note: This class is new in the .NET Framework version 2.0.

Defines and represents a dynamic method. This class cannot be inherited.

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

public ref class DynamicMethod sealed : public MethodInfo
/** @attribute ComVisibleAttribute(true) */ 
public final class DynamicMethod extends MethodInfo
public final class DynamicMethod extends MethodInfo

You can use the DynamicMethod class to generate and execute a method at run time, without having to generate a dynamic assembly and a dynamic type to contain the method. Dynamic methods are the most efficient way to generate and execute small amounts of code.

A dynamic method is logically associated with a module or with a type. If it is associated with a module, the dynamic method is effectively global to that module. With sufficient permissions, a dynamic method can skip just-in-time (JIT) visibility checks and access the private data of types declared in that module. You can associate a dynamic method with any module, whether or not you created the module.

If the dynamic method is associated with a type, it has access to the private members of the type. There is no need to skip JIT visibility checks, unless the dynamic method accesses private data of other types declared in the same module. You can associate a dynamic method with any type.

The following table shows which members of types in a module are accessible to a dynamic method, with and without JIT visibility checks, for dynamic methods associated with the module or with a type in the module.


Associated with module

Associated with type

JIT visibility checks

Public and internal members of public, internal, and private types.

All members of the associated type. Public and internal members of all the other types.

Skip JIT visibility checks

All members of all the types.

All members of all the types.

A dynamic method has the permissions of the module with which it is associated, or of the module containing the type with which it is associated.

Dynamic methods and their parameters do not have to be named, but you can specify names to assist in debugging. Custom attributes are not supported on dynamic methods or their parameters.

Although dynamic methods are static methods (Shared methods in Visual Basic), the relaxed rules for delegate binding introduced in the .NET Framework version 2.0 allow a dynamic method to be bound to an object, so that it acts like an instance method when called using that delegate instance. An example that demonstrates this is provided for the CreateDelegate(Type,Object) method overload.

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.

No code example is currently available or this language may not be supported.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0