Reflection Emit Sample
Sample Overview
This sample demonstrates how to use reflection emit. Reflection emit is a
runtime feature that allows code to create dynamic assemblies, modules and
types. These types can be created for the purpose of creating dynamic instances
of them and using them. Reflection emit can also be used to generate an
assembly and persist it to disk as an executable file or DLL.
The sample allows you several interactive options:
- Create and test a dynamic type.
- Create and save a type to disk.
- Create two dynamic assemblies and test them.
- Load the saved assembly from the second option and execute it.
The user can run the application multiple times to explore the different
options.
The following briefly describes the classes and technologies used by this
sample.
- Reflection.Emit
- AppDomain
- The sample uses the AppDomain type to create a
dynamic assembly in the sample's application domain. It does this by calling
DefineDynamicAssembly().
- AssemblyBuilder
- Used to build a
dynamic assembly. It can be used to create an assembly for immediate use, or
a dynamic assembly can be persisted to an assembly DLL or .exe file.
- AssemblyName
- Used to define the unique identity of an assembly. The sample uses this
type in the simplest possible way by giving the dynamic assembly the text
name "EmittedAssembly".
- ModuleBuilder
- Used to build a dynamic module in the dynamic assembly.
- TypeBuilder
- This Type-derived type is used to build a type dynamically. From it you
can request instances of the FieldBuilder, ConstructorBuilder,
and MethodBuilder types. Using these types, the sample generates a
complete type.
- FieldBuilder
- Used to create a field in a dynamic type.
- ConstructorBuilder
- Used to define a constructor in a dynamic type.
- MethodBuilder
- Used to define a method in a dynamic type.
- ILGenerator
- Both MethodBuilder and ConstructorBuilder implement a method called
GetILGenerator(). These methods return an instance of the ILGenerator type
which is used to dynamically generate IL-code for a type.
- Thread
- The static member function GetDomain() is used to get an instance of the
AppDomain type for the current thread.
Sample Source and Build Output Locations
The sample source is found in sscli\samples\howto\reflection\emit.
The source file is:
The build output location is %TARGETCOMPLUS%\samples\howto\reflection\emit.
The output file is an executable assembly named reflectionemit.exe.
Building the Sample
All samples are built from the buildall script.
You can also build all the
samples by switching to the root of the sample directory, sscli\samples, and typing
build -c.
You can build this specific sample by switching to the sample directory and typing
build -c.
Running the Sample
These steps require that the Shared Source CLI (SSCLI) be already built and
functional.
- Run env.bat or source the env.csh or env.sh script files depending on
your platform.
- Switch to the %TARGETCOMPLUS%\samples\howto\reflection\emit directory.
- Type the following command:
clix reflectionemit.exe
Copyright (c) 2002 Microsoft Corporation. All rights reserved.