System.Reflection.Emit.DynamicMethod Class

Defines and represents a dynamic method that can be compiled, executed, and discarded. Discarded methods are available for garbage collection.

See Also: DynamicMethod Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DynamicMethod : System.Reflection.MethodInfo

Remarks

You can use the System.Reflection.Emit.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. The executable code created by the just-in-time (JIT) compiler is reclaimed when the System.Reflection.Emit.DynamicMethod object is reclaimed. Dynamic methods are the most efficient way to generate and execute small amounts of code.

A dynamic method can be anonymously hosted, or it can be logically associated with a module or with a type.

The following table shows which types and members are accessible to an anonymously hosted dynamic method, with and without JIT visibility checks, depending on whether System.Security.Permissions.ReflectionPermission with the System.Security.Permissions.ReflectionPermissionFlag.RestrictedMemberAccess flag is granted.

 

Without System.Security.Permissions.ReflectionPermissionFlag.RestrictedMemberAccess

With System.Security.Permissions.ReflectionPermissionFlag.RestrictedMemberAccess

Without skipping JIT visibility checks

Public members of public types in any assembly.

Public members of public types in any assembly.

Skipping JIT visibility checks, with restrictions

Public members of public types in any assembly.

All members of all types, only in assemblies whose trust levels are equal to or less than the trust level of the assembly that emitted the dynamic method.

Note:

Prior to the net_v20sp1_long, emitting code required System.Security.Permissions.ReflectionPermission with the System.Security.Permissions.ReflectionPermissionFlag.ReflectionEmit flag. This permission is included by default in the FullTrust and LocalIntranet named permission sets, but not in the Internet permission set. Therefore, in earlier versions of the dnprdnshort a library can be used with Internet permissions only if it has the System.Security.SecurityCriticalAttribute attribute and also executes an System.Security.PermissionSet.Assert for System.Security.Permissions.ReflectionPermissionFlag.ReflectionEmit. Such libraries require careful security review because coding errors could result in security holes. The net_v20SP1_short allows code to be emitted in partial trust scenarios without issuing any security demands, because generating code is not inherently a privileged operation. That is, the generated code has no more permissions than the assembly that emits it. This allows libraries that emit code to be security transparent and removes the need to assert System.Security.Permissions.ReflectionPermissionFlag.ReflectionEmit, which simplifies the task of writing a secure library. To use this feature, your application should target the net_v35_long or later.

The following table shows which types and members are accessible to a dynamic method that is associated with a module or with a type in a module.

 

Associated with module

Associated with type

Without skipping JIT visibility checks

Public and internal members of public, internal, and private types in the module.

Public members of public types in any assembly.

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

Public members of public types in any assembly.

Skipping JIT visibility checks

All members of all types in any assembly.

All members of all types in any assembly.

A dynamic method that is associated with a module has the permissions of that module. A dynamic method that is associated with a type has the permissions of the module containing that type.

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 dnprdnlong 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 DynamicMethod.CreateDelegate(Type, object) method overload.

Note:

In the dnprdnlong, dynamic methods do not support symbol information, that is, local variable names and line-number mapping. This limitation might be removed in a future version. You can use System.Reflection.Emit.AssemblyBuilder during development to simplify debugging the generated Microsoft intermediate language (MSIL), and then switch to dynamic methods during final deployment, because the System.Reflection.Emit.ILGenerator calls are the same in both cases.

Verification

The following list summarizes the conditions under which dynamic methods can contain unverifiable code. (For example, a dynamic method is unverifiable if its DynamicMethod.InitLocals property is set to false.)

The exception that is thrown for unverifiable code varies depending on the way the dynamic method is invoked. If you invoke a dynamic method by using a delegate returned from the DynamicMethod.CreateDelegate(Type) method, a System.Security.VerificationException is thrown. If you invoke the dynamic method by using the erload:System.Reflection.Emit.DynamicMethod.Invoke method, a System.Reflection.TargetInvocationException is thrown with an inner System.Security.VerificationException.

Requirements

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