| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Load Assemblies into an Application Domain |
03/30/2017 |
.net-framework |
|
article |
|
1432aa2d-bd83-4346-bf3b-a1b7920e2aa9 |
16 |
rpetrusha |
ronpet |
wpickett |
How to: Load Assemblies into an Application Domain
There are several ways to load an assembly into an application domain. The recommended way is to use the static (Shared in Visual Basic) xref:System.Reflection.Assembly.Load%2A method of the xref:System.Reflection.Assembly?displayProperty=nameWithType class. Other ways assemblies can be loaded include:
-
The xref:System.Reflection.Assembly.LoadFrom%2A method of the xref:System.Reflection.Assembly class loads an assembly given its file location. Loading assemblies with this method uses a different load context.
-
The xref:System.Reflection.Assembly.ReflectionOnlyLoad%2A and xref:System.Reflection.Assembly.ReflectionOnlyLoadFrom%2A methods load an assembly into the reflection-only context. Assemblies loaded into this context can be examined but not executed, allowing the examination of assemblies that target other platforms. See How to: Load Assemblies into the Reflection-Only Context.
[!NOTE] The reflection-only context is new in the .NET Framework version 2.0.
-
Methods such as xref:System.AppDomain.CreateInstance%2A and xref:System.AppDomain.CreateInstanceAndUnwrap%2A of the xref:System.AppDomain class can load assemblies into an application domain.
-
The xref:System.Type.GetType%2A method of the xref:System.Type class can load assemblies.
-
The xref:System.AppDomain.Load%2A method of the xref:System.AppDomain?displayProperty=nameWithType class can load assemblies, but is primarily used for COM interoperability. It should not be used to load assemblies into an application domain other than the application domain from which it is called.
[!NOTE] Starting with the .NET Framework version 2.0, the runtime will not load an assembly that was compiled with a version of the .NET Framework that has a higher version number than the currently loaded runtime. This applies to the combination of the major and minor components of the version number.
You can specify the way the just-in-time (JIT) compiled code from loaded assemblies is shared between application domains. For more information, see Application Domains and Assemblies.
Example
The following code loads an assembly named "example.exe" or "example.dll" into the current application domain, gets a type named Example from the assembly, gets a parameterless method named MethodA for that type, and executes the method. For a complete discussion on obtaining information from a loaded assembly, see Dynamically Loading and Using Types.
[!code-cppSystem.AppDomain.Load#2] [!code-csharpSystem.AppDomain.Load#2] [!code-vbSystem.AppDomain.Load#2]
See Also
xref:System.Reflection.Assembly.ReflectionOnlyLoad%2A
Programming with Application Domains
Reflection
Using Application Domains
How to: Load Assemblies into the Reflection-Only Context
Application Domains and Assemblies