This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!
Reflection Emit Application Scenarios
This section describes some application scenarios for 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:
- 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 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.
- The script engine loads the dynamic assembly into a newly created application domain and executes the application domain. The code is executed in a separate application domain to isolate the code from the script engine.
Executing a Script in an ASP+ Page
In this scenario, an ASP+ page containing a script program is downloaded to a Web browser for the first time. The steps are as follows:
- ASP+ 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+ 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 same page is visited again.
Compiling a Regular Expression
In this scenario, a compiler uses the Reflection Emit APIs to compile regular expressions in source code. A user can declare which regular expressions should be compiled.
- 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 the Reflection Emit APIs since the user has requested that the expression be compiled. The regular expression is first translated to the compiler’s regular expression bytecodes. For example, one instruction might say, “try to match zero or more a’s.” The regular expression bytecodes are then translated to IL. For example, the compiler might generate a loop that iterates over all the a’s 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 since 30-500% speedup in execution time is possible.