Reflection Emit Application Scenarios
This section describes in more detail some of the scenarios in which applications use reflection emit and dynamic methods.
In this scenario, a Web browser loads an HTML page containing a script program and executes the program. The steps are as follows:
The Web browser launches the script engine in the Web browser's application domain.
The Web browser extracts the script from the HTML page and passes it to the script engine.
The script engine creates a transient dynamic assembly.
The script engine uses the reflection emit APIs to emit the code into the dynamic assembly.
In this scenario, a browser opens an ASP.NET page containing a script program. The program is compiled on the server and executed. The steps are as follows:
ASP.NET starts its application domain and launches the script engine to compile and execute the program.
The script engine creates a persistable dynamic assembly and a persistable dynamic module in the dynamic assembly.
The script engine compiles the program using the reflection emit APIs and emits the code into the persistable dynamic module.
The compiled code is run and an eval expression is encountered.
The script engine creates a transient dynamic module in the dynamic assembly to contain the code for the eval expression.
The script engine evaluates the eval expression by executing the transient dynamic module.
ASP.NET requests the script engine to save the dynamic assembly and the persistable dynamic module in the cache so that the script does not need to be recompiled when the page is visited again.
In this scenario, a compiler uses reflection emit to compile regular expressions in source code. A user can declare which regular expressions should be compiled. The steps are as follows:
The compiler processes the first use of a regular expression in a user's source code.
The compiler compiles the regular expression into a custom scanner class using reflection emit because the user has requested that the expression be compiled. The regular expression is first translated to the compiler's regular expression opcodes. For example, one instruction might say, "try to match zero or more a's." The regular expression opcodes are then translated to Microsoft intermediate language (MSIL). For example, the compiler might generate a loop that iterates over all the occurrences of the letter a that it found.
The compiler saves the compiled scanner class for future use.
The compiler instantiates the compiled scanner class and executes the code in the current application domain.
The compiler processes a subsequent use of the same regular expression in the user's source code.
The compiler retrieves the compiled scanner class for the regular expression, instantiates it, and executes the code in the current application domain.
The user incurs a performance penalty when the first instance of a regular expression is used because the compiler needs to compile the expression into a scanner class. However, subsequent uses of the same regular expression are executed efficiently. For regular expressions that are used frequently, the one-time compilation penalty is insignificant because a significant improvement in execution time is possible.