Click here to Skip to main content
15,914,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i add an assembly(dll) as reference in a C# project at run time.?! i need to add a reference at run time.!? i.e normally we add references by Right-Clicking References in Solution Explorer and we can browse for any dll and add it in references. i need to do this using code.
Posted

I guess what you want is load an Assembly[^] at run time.
Assembly.Load[^]
Assembly.LoadFrom[^]
Using Reflection[^] you can loop through its types and instantiate Objects from it, possibly using the Activator Class[^] and Activator.CreateInstance Method[^].
Hope it helps :)
 
Share this answer
 
Comments
fjdiewornncalwe 23-Nov-11 15:28pm    
I was actually just looking for a project where I did this a while back to ensure I remembered correctly how to do this. Your answer is exactly what worked.
Sander Rossel 23-Nov-11 15:35pm    
Glad to be of help :)
Sergey Alexandrovich Kryukov 23-Nov-11 22:38pm    
I agree, voted 5. However, there are some aspects which makes it more complex than it may seem.
Please see my solution.
--SA
Sander Rossel 24-Nov-11 2:28am    
Thanks :)
Mehdi Gholam 24-Nov-11 1:52am    
5'ed
The solution by Naerling is correct: the referenced assembly is a static thing; all you can do it loading assembly during run-time and using Reflection. Unfortunately, you cannot unload such assembly, is you need something like reloadable plugins, you will also need to load assembles in a separate Application Domain and use IPC, as Application Domains are isolated from each other the same was as separate processes. You can unload only the whole Application Domain with all its code linked statically or during run-time.

For more detail, see my past solutions:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Dynamically Load User Controls[^].

—SA
 
Share this answer
 
v2
Comments
Mehdi Gholam 24-Nov-11 1:52am    
5'ed
Sergey Alexandrovich Kryukov 24-Nov-11 1:59am    
Thank you, Mehdi.
--SA
Sander Rossel 24-Nov-11 2:33am    
Interesting. Never used AppDomains myself, but as I read they can be pretty handy in cases like these. 5ed.
Sergey Alexandrovich Kryukov 24-Nov-11 2:36am    
It's critically important for this very case (unloading an assembly) -- there is absolutely no another way.
Thank you, Naerling.
--SA
Sander Rossel 24-Nov-11 2:57am    
I think I'll have another look at AppDomain sometime :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900