Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Loading Assemblies into an Application Domain

In the .NET Framework, there are several ways to load an assembly into an application domain. Each way uses a different class.

You can use the following overloaded methods to load an assembly into an application domain:

  • The System.AppDomain class contains several overloaded Load methods. These methods are primarily used for COM interoperability, although they can be used to successfully load any assembly into the current or a new application domain. You can also load an assembly using the CreateInstance methods.
  • The System.Reflection.Assembly class contains two static overloaded methods, Load and LoadFrom. The two methods vary by load context.

The following example loads an assembly into the current application domain and then executes the assembly. For a complete discussion on obtaining information from a loaded assembly, see Dynamically Loading and Using Types.

Imports System
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].LoadFrom("adname.exe")
      'Get the type to use.
      myType = a.GetType("adname")
      'Get the method to call.
      mymethod = myType.GetMethod("adnamemethod")
      'Create an instance
      obj = Activator.CreateInstance(myType)
      'Execute the adnamemethod method.
      mymethod.Invoke(obj,null)
   End Sub 'Main
End Class 'Asmload0
[C#]
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.LoadFrom("adname.exe");
   //Get the type to use.
   Type myType = a.GetType("adname");
   //Get the method to call.
   MethodInfo mymethod = myType.GetMethod("adnamemethod");
   //Create an instance.
   Object obj = Activator.CreateInstance(myType);
   //Execute the adnamemethod method.
   mymethod.Invoke(obj,null);
   }
}

See Also

Hosting the Common Language Runtime | Programming with Application Domains | Reflection Overview | Using Application Domains

Show:
© 2015 Microsoft