tags:

views:

444

answers:

4

My project is an application in which we load various assemblies and perform operations on them.

We are stuck at a situation where we need to add a reference to the assembly we load (which will be selected by user). So I need to add a reference to the DLL at run time.

I tried this site but here they support only microsoft DLLs like System.Security etc. I want to add a reference to a user created dll (class library).

A: 

If you load an assembly at runtime, it will look for all its dependencies in the current location or the GAC, and load them if found, else error.

leppie
+6  A: 

You can't "add a reference" at runtime - but you can load assemblies - Assembly.LoadFrom / Assembly.LoadFile etc. The problem is that you can't unload them unless you use AppDomains. Once you have an Assembly, you can use assemblyInstance.GetType(fullyQualifiedTypeName) to create instances via reflection (which you can then cast to known interfaces etc).

For a trivial example:

// just a random dll I have locally...
Assembly asm = Assembly.LoadFile(@"d:\protobuf-net.dll");
Type type = asm.GetType("ProtoBuf.ProtoContractAttribute");
object instance = Activator.CreateInstance(type);

At which point I can either cast instance to a known base-type/interface, or continue to use reflection to manipulate it.

Marc Gravell
its not working type is empty
Arunachalam
Then you haven't given it the correct fully-qualified name, or it doesn't exist; Assembly.GetType() will find even private types, etc. Note that generics and nested types get slightly different syntax to what you see in C#.
Marc Gravell
what is ProtoBu men here is it the dll name . i gave my dll name and the class which i require
Arunachalam
Assembly asm = Assembly.LoadFile(@"C:\Documents and Settings\E454930\Desktop\nunit_dll_hutt\for_hutt_proj\bin\Debug\for_hutt_proj.dll"); Type type = asm.GetType("for_hutt_proj.int"); object instance = Activator.CreateInstance(type);this is what i did what is mistake here
Arunachalam
A: 

The Composite UI Application Block facilitates the design and implementation of your client applications in three areas:

  • It allows your application to be based on the concept of modules or plug-ins.
  • It allows developers with shell expertise to build components that hide user interface complexity from the business logic development.
  • It facilitates development using patterns for loose coupling between modules.

For WPF: Take a look at Prism: patterns & practices Composite Application Guidance for WPF and Silverlight site It does the assembly loading you require and actually uses Unity internally as it's IoC container.

For non WPF: Take a look at Smart Client - Composite UI Application Block

Or alternatively: Try any of the IoC containers like Castle Windsor, autofac, unity, etc.

Simon Hughes
+1  A: 

If the assembly is in another location than the current or in the GAC, just use the AppDomain.CurrentDomain.AssemblyResolve event to deliver the assembly yourself.

Wolf5