Accessing Members of Dynamically Loaded Assemblies

J

Jordan S.

I plan to load an assembly during application startup, and load that
assembly via reflection (i.e., it isn't referenced in the application's
assembly manifest). The assembly will be loaded into the application's
default app domain, with code like this:

using System.Reflection;
Assembly loadedAssembly = Assembly.LoadFile(pathToMyAssembly);

My question: When I need to access members of the assembly [loaded via
reflection], how does the application know to get the member from the
dynamically loaded assembly [that is already loaded]? Am I required to get a
runtime reference to that dynamically loaded assembly? and execute GetType()
on that assembly reference?
Or will the Framework attempt to locate the assembly by first looking into
the current app domain before going to the assembly manifest to try to load
the assembly?

I have read up on assembly binding and probing rules and heuristics, but
what I have read assumes that the assembly is listed in the application's
assembly manifest... looks there, then goes through the normal probing
sequence. Is it possible that I've missed something ? and that the "normal
probing sequence" actually starts with the application's default application
domain and uses any loaded assembly before searching the GAC, /bin,
<probing> locations, etc?

Thanks.
 
A

Arne Vajhøj

Jordan said:
I plan to load an assembly during application startup, and load that
assembly via reflection (i.e., it isn't referenced in the application's
assembly manifest). The assembly will be loaded into the application's
default app domain, with code like this:

using System.Reflection;
Assembly loadedAssembly = Assembly.LoadFile(pathToMyAssembly);

My question: When I need to access members of the assembly [loaded via
reflection], how does the application know to get the member from the
dynamically loaded assembly [that is already loaded]? Am I required to get a
runtime reference to that dynamically loaded assembly? and execute GetType()
on that assembly reference?

If any class you load already has a reference to that assembly then
there are no need to load it this way.

If you load it this way you will want to do something like:

IFoobar o = (IFoobar)loadedAssembly.CreateInstance("FoobarA");

where IFoobar is an interface the app knows and FoobarA is
an implementation that the app does not know.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top