共用方式為


Reflection Emit Dynamic Method Scenarios

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Dynamic methods, which are created by using the DynamicMethod class, provide enhanced capabilities for emitting static methods at run time. Dynamic methods expand the functionality of the types in the System.Reflection.Emit namespace in several ways:

  • They have less overhead, because there is no need to generate dynamic assemblies, modules, and types to contain the methods.

  • They provide better resource utilization because the memory used by method bodies can be reclaimed when the method is no longer needed.

Dynamic methods can use an ILGenerator object to emit Microsoft intermediate language (MSIL).

Dynamic methods are useful in scenarios where run-time code generation is required for performance. Examples discussed in this topic include serialization, mappings between objects and relational databases, regular expressions, partial evaluation, and compilers for languages that require runtimes.

For a simple example of dynamic method generation, see How to: Define and Execute Dynamic Methods.

Languages That Support Late-Bound Calls

Dynamic methods are useful to compiler writers when the type of an object is not known at compile time. Calls to the object's members must be resolved at run time, frequently with the additional overhead of manipulating argument lists. The following Visual Basic code provides an example of this.

Sub Example(ByVal obj as Object)
    ' ...
    obj.SomeMethod(x, y, z)
    ' ...
End Sub

The compiler must generate code to look up SomeMethod, prepare the arguments as an array of objects, and invoke the method. Executing such calls with reflection, using the InvokeMember method, might not provide the desired level of performance. If a call is made multiple times, performance can be improved by emitting a dynamic method to make the call. Alternatively, you can create a dynamic assembly, module, type, and method to make the call, but the resulting assembly can be unloaded only by unloading the application domain. By contrast, the memory used by dynamic methods is reclaimed when the methods are no longer needed.

Generated methods can be cached if used frequently, or they can simply be released.

Partial Evaluation

Partial evaluation, also known as program specialization, is a technique for optimizing algorithms where one or more of the input variables vary more slowly than the other inputs. Partial evaluation generates specialized method calls that treat the values of the slowly varying inputs as if they were constant, allowing additional optimizations to be applied to the algorithm as a whole.

Using this technique, it is often possible to transform a low-performance, general-purpose algorithm into a high-performance, specialized algorithm. The following are some examples:

  • Compiling a Regex object to generate a program that is specialized to match a particular pattern.

  • Compiling an XML schema to generate a program that is specialized to validate a particular schema.

  • Compiling an XSLT transformation into a program that is specialized to transform an XML document in a specific way.

  • Compiling a generic encryption program that encrypts data using any specified key into a program that is optimized for a particular key.

Dynamic methods can be used to implement partial evaluation by generating specialized methods at run time. In addition to performance improvements, dynamic methods allow reclamation of the MSIL method bodies and the related machine code produced by the just-in-time (JIT) compiler.

Generating Custom User Code at Run Time

Many applications have extensibility mechanisms that give users the ability to write and execute custom code while the application is running, frequently through the use of predefined functions. By using dynamic methods to generate this code, the application designer can reduce the number of functions required (and hence the memory footprint) and provide more flexibility for the user without sacrificing performance.