다음을 통해 공유


Dynamic Type Resolution Technology Sample

This sample demonstrates how hosts can participate in the type resolution process by supplying an event handler that returns an assembly containing the requested type. This is an advanced technique, most likely to be used in distributed applications to facilitate flexibility and fault-tolerance.

For information about using the samples, see the following topics:

To build the sample using the command prompt

  1. Open a Command Prompt window and navigate to one of the language-specific subdirectories for the sample.

  2. Type msbuild DynamicTypeResolveCS.sln or msbuild DynamicTypeResolveVB.sln, depending on your choice of programming language, at the command line.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories for the sample.

  2. Double-click the icon for DynamicTypeResolveCS.sln or DynamicTypeResolveVB.sln, depending on your choice of programming language, to open the file in Visual Studio.

  3. On the Build menu, click Build Solution.

To run the sample

  1. Open the Command Prompt window and navigate to the directory that contains the built executable file.

  2. Type DynamicTypeResolve.exe at the command line.

NoteNote

The sample builds a console application. You must launch and run it in a Command Prompt window to view its output.

Remarks

The sample attempts to instantiate a non-existent type. However, the sample also handles type-load failures, and dynamically creates the necessary type, avoiding a FileNotFoundException.

The sample uses the following technologies and classes.

  • Reflection

    • AppDomain The AssemblyResolve event of this class is used to add an event-handler that is called when a type load fails.

    • Activator Used to create instances of a type whose name is not necessarily known at compile time.

    • MethodInfo **** Used to invoke a method on a type, where the method name is not necessarily known at compile time.

    • AssemblyBuilder Used to emit a dynamic assembly at run time.

    • AssemblyName **** Used to define the unique identity of an assembly. The sample makes the simplest use of this type, to give the dynamic assembly a text name.

    • ModuleBuilder Used to build a dynamic module in the dynamic assembly.

    • TypeBuilder **** This type, derived from 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.

    • MethodBuilder Used to define a method in a dynamic type.

    • ILGenerator The MethodBuilder type implements the GetILGenerator method, which returns an instance of the ILGenerator type. This type is used to generate Microsoft Intermediate Language (MSIL) code dynamically.

  • Delegates and Events

    • ResolveEventHandler Used to indicate a callback method to be called when a type-resolution fails.

See Also

Reference

Activator
AppDomain
AssemblyBuilder
AssemblyBuilderAccess
AssemblyName
FileNotFoundException
ILGenerator
MethodAttributes
MethodBuilder
MethodInfo
ModuleBuilder
ResolveEventHandler
System.IO
System.Reflection
System.Reflection.Emit
TypeBuilder

Other Resources

Reflection
Emitting Dynamic Methods and Assemblies
Dynamic Source Code Generation and Compilation
Handling and Raising Events
Managed and Unmanaged Events