Mahsa
Mahsa

Reputation: 373

Add DLL programmatically at runtime

Using C#, I create a DLL at runtime and now I want to add it as a reference to my project at runtime.

I tried using the LoadFrom method, but it doesn't work.

How can I do this?

Upvotes: 21

Views: 38659

Answers (7)

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

First you should load the dll

Assembly assembly = Assembly.LoadFrom("dllPath");

Then you may need to add the assembly to the app domain

AppDomain.CurrentDomain.Load(assembly.GetName());

After that you can load any type from this assembly

Type t = assembly.GetType("typeName");

Then using reflection you can execute methods on this type

Note that you may need to add the below in the configuration file.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <probing privatePath="dlls folder"/>
  </assemblyBinding>
</runtime>

Upvotes: 27

Ohad Schneider
Ohad Schneider

Reputation: 38112

Actually Assembly.Load is usually what you'd want, not LoadFrom and not LoadFile:

Which context is right for you? In general, I strongly recommend that you use the Load context whenever possible

http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx

Upvotes: 2

stian.net
stian.net

Reputation: 3963

LoadFile vs. LoadFrom

Be careful - these aren't the same thing.

LoadFrom() goes through Fusion and can be redirected to another assembly at a different path but with that same identity if one is already loaded in the LoadFrom context. LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly* what the caller requested. It doesn't use either the Load or the LoadFrom context. So, LoadFrom() usually gives you what you asked for, but not necessarily. LoadFile() is for those who really, really want exactly what is requested. (*However, starting in v2, policy will be applied to both LoadFrom() and LoadFile(), so LoadFile() won't necessarily be exactly what was requested. Also, starting in v2, if an assembly with its identity is in the GAC, the GAC copy will be used instead. Use ReflectionOnlyLoadFrom() to load exactly what you want - but, note that assemblies loaded that way can't be executed.)

LoadFile() has a catch. Since it doesn't use a binding context, its dependencies aren't automatically found in its directory. If they aren't available in the Load context, you would have to subscribe to the AssemblyResolve event in order to bind to them.

ref Suzanne Cook's .NET CLR Notes

Upvotes: 9

hungryMind
hungryMind

Reputation: 6999

You cannot add dll to a project when project is already running. However, you can load the dll using Assembly.LoadFrom( filename). Normally such scenerio is used for SOA or plugin based projects. You can use interface to specify the type structure and load the dll and use it.

Upvotes: 1

Joel Martinez
Joel Martinez

Reputation: 47749

This is very simple in .NET: http://msdn.microsoft.com/en-us/library/1009fa28.aspx

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

You could use the Assembly.LoadFrom method to dynamically load an assembly at runtime.

Upvotes: 0

dzendras
dzendras

Reputation: 4751

Use Assembly.LoadFile method and then run code inside it using reflection.

Upvotes: 4

Related Questions