AssemblyLoad event being fired on GetTypes call

Hi,

Is there any scenario wherein calling GetTypes( ) on one of the loaded assembly result in firing AssemblyLoad event

I am seeing this behavior in one of my application but its hard to justify why CLR will ever do this. Overall the application is fairly complex, but the code sub-snippet is something like this:

Registered to AssemblyLoad event - I have a cache variable to store all of the loaded assembly types which is reset to null whenever the AssemblyLoad event occurs

I have another method : LoadTypes() as follows:

void LoadTypes()

{

HashMap h = new HashMap();

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

for(int i = 0; i < assemblies.length; i++) {

Type[] types = assembliesIdea.GetTypes();

}

}

For one of my assembly at the GetTypes() execution, the execution flow goes into AssemblyLoadEventHandler. I am unable to digest this behavior. Can someone explain me this CLR behavior




Answer this question

AssemblyLoad event being fired on GetTypes call

  • billg51

    Sure, say you have a class B in one of your loaded assemblies which inherits from A, and A is in a seperate DLL. If you haven't used B or any types from the DLL that A is defined in, then that DLL won't be loaded. By calling GetTypes on the assembly containing B, the CLR must find out about type B, which requires finding out about its base type A. As the DLL containing A hasn't been loaded yet, the CLR must now load it.

    It could be the same if you have a field which in one of the types which is of a type defined in an assembly that hasn;t been loaded yet.


  • Jens-Christian Larsen

    Thanks for the answer. I was tried around the same lines but was playing with single type scattered in 2 dlls hence both the dll's gets loaded at same time.

    Bow I had two types in first dll, first is independent of anything else and second type implements other class available in 2nd dll. This does the trick.



  • AssemblyLoad event being fired on GetTypes call