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) Load method of the System.Reflection.Assembly class. Other ways assemblies can be loaded include:

Note

The reflection-only context is new in .NET Framework version 2.0.

  • Methods such as CreateInstance and CreateInstanceAndUnwrap of the AppDomain class can load assemblies into an application domain.

  • The GetType method of the Type class can load assemblies.

  • The Load method of the System.AppDomain 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 .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.

using namespace System;
using namespace System::Reflection;

public ref class Asmload0
{
public:
    static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly^ a = Assembly::Load("example");
        // Get the type to use.
        Type^ myType = a->GetType("Example");
        // Get the method to call.
        MethodInfo^ myMethod = myType->GetMethod("MethodA");
        // Create an instance.
        Object^ obj = Activator::CreateInstance(myType);
        // Execute the method.
        myMethod->Invoke(obj, nullptr);
    }
};

int main()
{
    Asmload0::Main();
}
using System;
using System.Reflection;

public class Asmload0
{
    public static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }
}
Imports System.Reflection

Public Class Asmload0
    Public Shared Sub Main()
        ' Use the file name to load the assembly into the current
        ' application domain.
        Dim a As Assembly = Assembly.Load("example")
        ' Get the type to use.
        Dim myType As Type = a.GetType("Example")
        ' Get the method to call.
        Dim myMethod As MethodInfo = myType.GetMethod("MethodA")
        ' Create an instance.
        Dim obj As Object = Activator.CreateInstance(myType)
        ' Execute the method.
        myMethod.Invoke(obj, Nothing)
    End Sub
End Class

See also