Reflection Emit Application Scenarios

This section describes some of the scenarios in which applications use reflection emit.

Executing a Script in a Web Browser

In this scenario, a Web browser loads an HTML page containing a script program and executes the program. The steps are as follows:

  1. The Web browser launches the script engine in the Web browser's application domain.
  2. The Web browser extracts the script from the HTML page and passes it to the script engine.
  3. The script engine creates a transient dynamic assembly.
  4. The script engine uses the reflection emit APIs to emit the code into the dynamic assembly.

Executing a Script in an ASP.NET Page

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:

  1. ASP.NET starts its application domain and launches the script engine to compile and execute the program.
  2. The script engine creates a persistable dynamic assembly and a persistable dynamic module in the dynamic assembly.
  3. The script engine compiles the program using the reflection emit APIs and emits the code into the persistable dynamic module.
  4. The compiled code is run and an eval expression is encountered.
  5. The script engine creates a transient dynamic module in the dynamic assembly to contain the code for the eval expression.
  6. The script engine evaluates the eval expression by executing the transient dynamic module.
  7. 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.

Compiling a Regular Expression Using Reflection Emit

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:

  1. The compiler processes the first use of a regular expression in a user's source code.
  2. 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.
  3. The compiler saves the compiled scanner class for future use.
  4. The compiler instantiates the compiled scanner class and executes the code in the current application domain.
  5. The compiler processes a subsequent use of the same regular expression in the user's source code.
  6. 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.

See Also

Reflection Emit Scenarios | .NET Framework Regular Expressions