470,641 Members | 1,536 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Dynamic Assembly Loading

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
Nov 16 '05 #1
2 20374
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) ;
}
}

"Stelios" <st************@avantisworld.com> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #2
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.
"Stelios" <st************@avantisworld.com> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Parag Joshi | last post: by
reply views Thread by Thomas W. Brown | last post: by
2 posts views Thread by Matt | last post: by
3 posts views Thread by dhnriverside | last post: by
4 posts views Thread by Barry Kelly | last post: by
4 posts views Thread by BrianS | last post: by
reply views Thread by Stefan Rosenthal | last post: by
8 posts views Thread by George Meng | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.