none
Assembly load notification

    Question

  • Is there a way for an assembly to be notified when it is being loaded? I have read about using a static constructor to determine this, but I feel this is a kludge. Preferably, it would be nice to determine what is loading the assembly as well. I would expect that this notification would be performed before any other executable code was run in the assembly (preferably).

    Thanks,
    Tomas
    Wednesday, April 11, 2007 4:28 PM

Answers

  • You can declare a global class constructor which is called whenever something inside the assembly is accessed the first time. The drawback is that you cannot declare a global class constructur within any high level language. The solution is only applicable to MSIL (Microsoft Intermediate Language) code.

    E..g
    This method must be declared outside any class scope
    .method private static void .cctor( )
    {

    ....

    }


    Yours,
       Alois Kraus

    Wednesday, April 11, 2007 11:04 PM

All replies

  • I don't believe there is any way to do this. Even a static constructor would not be reliable, as static constructors are not run until the first time a type is referenced. Maybe if you can explain why you want to do this, we can help you come up with an alternative solution.
    Wednesday, April 11, 2007 6:33 PM
    Moderator
  • Indian Ocean:

     

    The links you posted notify the AppDomain that an assembly has been loaded. However, since no managed code from the loaded assembly can run prior to the AssemblyLoad event being raised, the loaded assembly has no opportunity to attach to that event to receive the notification.

    Wednesday, April 11, 2007 7:11 PM
    Moderator
  • You can declare a global class constructor which is called whenever something inside the assembly is accessed the first time. The drawback is that you cannot declare a global class constructur within any high level language. The solution is only applicable to MSIL (Microsoft Intermediate Language) code.

    E..g
    This method must be declared outside any class scope
    .method private static void .cctor( )
    {

    ....

    }


    Yours,
       Alois Kraus

    Wednesday, April 11, 2007 11:04 PM