Dynamic Assembly Loading

S

Stelios

Does anyone know how to dynamically load an assembly from a location other
that the working directory by using Assembly load method.
It is important to use the specific method because is the method used by CLR
to enforce version and security checking.

I have try by changing the appdomain APPBASE property but is not working and
i am wondering weather I am doing something wrong.

The code I use is the following

AppDomain currentDomain = AppDomain.CurrentDomain;

currentDomain.SetupInformation.ApplicationBase = "Location of hte assembly
to be loaded";

currentDomain.Load("AssemblyName;");



Thanks in advance
 
G

Greg Young

Assembly.Load(fullpath)

the problem is that the assembly you load must have its dependancies either
in your path/gac/etc.

example ... this loads assemblies not in app path.

public void LoadPluginDirectory(string _Path) {
try {
string [] Files = System.IO.Directory.GetFiles(_Path) ;
foreach (string CurrentFile in Files) {
try {
this.LoadAssembly(CurrentFile) ;
}
catch (Exception Ex) {
throw new System.Exception("Unable to load " + CurrentFile, Ex) ;
}
}
}
catch (Exception Ex) {
throw new System.Exception("Unable to get files from directory\n" +
Ex.Message, Ex) ;
}
}
 
D

David Levine

There are several problems here. First, you cannot change the appbase of an
appdomain after it has been created. You must specify it as part of the
configuration before creating a new one, which wont help you i the default
appdomain.

If the assembly you want to load is in a directory beneath the current
appbase then you can add its relative path using the
AppendPrivatePath(relativePath), and then use Assembly.Load - the runtime
will probe the subdirectories for you. This is the best/easiest way to
handle it.

If the assembly is located somewhere other then beneath the appbase then you
will have to manually load the assembly using LoadFrom, LoadFile, or some
other Loadxxx variant. You will probably also need to hook the
AssemblyResolve event of the appdomain to resolve references made to types
with the assembly as the program executes. The downside of using any of the
Loadxxx APIs is that assemblies loaded using them do not get added to the
context the runtime uses to automatically resolve references.
 

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