Loading referenced assemblies using Reflection  
Author Message
Antaris





PostPosted: .NET Base Class Library, Loading referenced assemblies using Reflection Top

Hi All,

I am in the process of alpha testing my persistent objects framework for .NET. I have recently stumbled upon a scenario that I hadn't accounted for earlier when dynamically loading types with Reflection. The situation that is occuring is that when an assembly is being loaded, and it references other assemblies, how can I get it to load the types from the referenced assemblies without throwing a ReflectionTypeLoadException. I realise you can use GetReferencedAssemblies(), but I don't know how to connect the two.

Any and all help is appreciated, many thanks

Matthew Abbott



.NET Development34  
 
 
Alexey Raga





PostPosted: .NET Base Class Library, Loading referenced assemblies using Reflection Top

Normally, when you use a method

Assembly.LoadFile

all the referenced assemblies should be loaded in memory automatically so you don't have to care about them.

And yes, you can enumerate all the referenced assemblies using GetReferencedAssemblies method and then enumerate all the types...

Or what is the problem exactly



 
 
VikasGoyal





PostPosted: .NET Base Class Library, Loading referenced assemblies using Reflection Top

Hi Matt,

Just a word of caution for you here .. use dynamic loading only if its absolutely required as it impacts the performance in a huge way for large number of calls.

you can read more about my findings here :

http://dotnetwithme.blogspot.com/2006/12/dynamic-binding-performance.html

http://DotNetWithMe.blogspot.com
vikas goyal



 
 
nobugz





PostPosted: .NET Base Class Library, Loading referenced assemblies using Reflection Top

An assembly gets loaded when you first reference a type from that assembly in your code. GetReferencedAssemblies() might be helpful to find out what assemblies *might* be loaded later but not what assemblies *will* get loaded. If the assembly loads its own assembly, you can't find out of course. If you want to get an early exception, try something like this:

AssemblyName[] asms = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
foreach (AssemblyName asm in asms)
Assembly.Load(asm);