다음을 통해 공유


Loading and Executing User Code

The reason for writing a host is to set up an application environment in which to run managed user code. In this context, user code means any managed code that is not specifically part of the host. For example, to the Internet Explorer host, user code is the managed controls and script that comprise the HTML pages. To an application server host, user code is the code that contains a corporation's business rules that the application server manages and executes.

All managed code is part of an Assembly class. As a result, the methods available to load and run managed code are all based on assemblies. For example, the System.AppDomain, and System.Reflection.Assembly classes contain methods that enable a host to load an assembly. Load methods come in various forms: some take an assembly name, some take a full file system path to the file containing the assembly manifest, and so on. These methods are used to load assemblies that have been created previously and are saved to disk.

For example, suppose that the application server host just described enables users to write managed-code business rules to be loaded into the application server process and run. When a request to run a method on a particular business rule comes into the application server, the server runtime hosting code determines in which domain to run the code, or whether a new domain must be created. The runtime hosting code then uses one of the assembly Load methods to load the assembly containing the business rule and uses reflection to execute a method on that business rule. For more information, see the documentation for the System.Reflection namespace.

The System.Reflection.Emit namespace also provides types for dynamically creating assemblies. Loading assemblies in this way is useful if the application is processing script code.

For example, a word-processing program might support a macro language with which users can customize the behavior of the application. After loading the runtime and creating an application domain, the word processing program might compile the macro scripts into managed code and create an assembly using System.Reflection.Emit. The created assembly can then be loaded into the application domain and run. Depending on the exact scenario, the assembly might only exist for the lifetime of the application (that is, never be saved to disk).

See Also

Hosting the Common Language Runtime | AppDomain Class | System.Reflection.Assembly | System.Reflection.Emit Namespace