This documentation is archived and is not being maintained.

AppDomain.AssemblyLoad Event

Occurs when an assembly is loaded.

[Visual Basic]
Public Overridable Event AssemblyLoad As AssemblyLoadEventHandler
[C#]
public virtual event AssemblyLoadEventHandler AssemblyLoad;
[C++]
public: virtual __event AssemblyLoadEventHandler* AssemblyLoad;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type AssemblyLoadEventArgs containing data related to this event. The following AssemblyLoadEventArgs property provides information specific to this event.

Property Description
LoadedAssembly Gets an Assembly that represents the currently loaded assembly.

Remarks

The AssemblyLoadEventHandler delegate for this event indicates what assembly was loaded.

To register an event handler for this event, you must have the permissions described in the Permissions section. If you do not have the appropriate permissions, a SecurityException occurs.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following sample demonstrates the AssemblyLoad event.

[Visual Basic, C#, C++] For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

[Visual Basic] 
Option Strict On
Option Explicit On

Imports System
Imports System.Reflection

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      AddHandler currentDomain.AssemblyLoad, AddressOf MyAssemblyLoadEventHandler
      
      PrintLoadedAssemblies(currentDomain)
      ' Lists mscorlib and this assembly

      ' You must supply a valid fully qualified assembly name here.      
      currentDomain.CreateInstance("System.Windows.Forms,Version,Culture,PublicKeyToken", "System.Windows.Forms.TextBox")
      ' Loads System, System.Drawing, System.Windows.Forms
      
      PrintLoadedAssemblies(currentDomain)
      ' Lists all five assemblies
   End Sub 'Main
   
   Sub PrintLoadedAssemblies(domain As AppDomain)
      Console.WriteLine("LOADED ASSEMBLIES:")
      Dim a As System.Reflection.Assembly
      For Each a In domain.GetAssemblies()
         Console.WriteLine(a.FullName)
      Next a
      Console.WriteLine()
   End Sub 'PrintLoadedAssemblies
   
   Sub MyAssemblyLoadEventHandler(sender As Object, args As AssemblyLoadEventArgs)
      Console.WriteLine("ASSEMBLY LOADED: " + args.LoadedAssembly.FullName)
      Console.WriteLine()
   End Sub 'MyAssemblyLoadEventHandler

End Module 'Test 

[C#] 
using System;
using System.Reflection;

class Test {

   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.AssemblyLoad += new AssemblyLoadEventHandler(MyAssemblyLoadEventHandler);
      
      PrintLoadedAssemblies(currentDomain);
      // Lists mscorlib and this assembly

      // You must supply a valid fully qualified assembly name here.      
      currentDomain.CreateInstance("System.Windows.Forms, Version, Culture, PublicKeyToken", "System.Windows.Forms.TextBox");
      // Loads System, System.Drawing, System.Windows.Forms
      
      PrintLoadedAssemblies(currentDomain);
      // Lists all five assemblies
   }
   
   static void PrintLoadedAssemblies(AppDomain domain) {
      Console.WriteLine("LOADED ASSEMBLIES:");
      foreach (Assembly a in domain.GetAssemblies()) {
         Console.WriteLine(a.FullName);
      }
      Console.WriteLine();
   }
   
   static void MyAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args) {
      Console.WriteLine("ASSEMBLY LOADED: " + args.LoadedAssembly.FullName);
      Console.WriteLine();
   }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

__gc class Test {
public:
   static void MyAssemblyLoadEventHandler(Object* sender, AssemblyLoadEventArgs* args) {
      Console::WriteLine(S"ASSEMBLY LOADED: {0}", args->LoadedAssembly->FullName);
      Console::WriteLine();
   }
};

void PrintLoadedAssemblies(AppDomain* domain) {
   Console::WriteLine(S"LOADED ASSEMBLIES:");
   System::Collections::IEnumerator* myEnum = domain->GetAssemblies()->GetEnumerator();
   while (myEnum->MoveNext()) {
      Assembly* a = __try_cast<Assembly*>(myEnum->Current);

      Console::WriteLine(a->FullName);
   }
   Console::WriteLine();
}

int main() {
   AppDomain*  currentDomain = AppDomain::CurrentDomain;
   currentDomain->AssemblyLoad += new AssemblyLoadEventHandler(currentDomain, Test::MyAssemblyLoadEventHandler);

   PrintLoadedAssemblies(currentDomain);
   // Lists mscorlib and this assembly

   // You must supply a valid fully qualified assembly name here.
   currentDomain->CreateInstance(S"System.Windows.Forms, Version, Culture, PublicKeyToken", S"System.Windows.Forms.TextBox");
   // Loads System, System::Drawing, System::Windows::Forms

   PrintLoadedAssemblies(currentDomain);
   // Lists all five assemblies
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

AppDomain Class | AppDomain Members | System Namespace

Show: