19

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?

5
  • what is your need to create/add dll at run-time? Jan 17, 2011 at 8:29
  • How are you creating a DLL at run-time?
    – Cody Gray
    Jan 17, 2011 at 8:31
  • You want to add the reference to the VS project at runtime? Makes no sense.
    – leppie
    Jan 17, 2011 at 8:33
  • 1
    This question is nonsense. You cannot dynamically create an assembly at runtime and use its types before you start the program. That requires a time machine. Jan 17, 2011 at 9:14
  • I have two exe. first one change the dll and then the second exe run and use that dll . the second exe has the previous version of dll . and I want to reload it in load event of second exe , and then run it
    – Mahsa
    Jan 17, 2011 at 9:28

7 Answers 7

26

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>
5
  • thanks Ghyath Serhal, would you please explain it more? where can I change My configuration
    – Mahsa
    Jan 17, 2011 at 9:38
  • First What is your application, is it an web application, windows application, windows service? Jan 17, 2011 at 9:50
  • Look, each application has an appdomain assosiated with it.In order to access a dll in a certain application, you should add the dll to its appdomain. When you add a dll to the appdomain, you cannot update it or remove it, and you cannot add the dll many time to the appdomain. Hence, the solution is whenever you change something in the dll, you should create a new dll, you can simply change it is name. after creating the new dll, you can load it, add it to the appdomain and use it. Jan 17, 2011 at 10:02
  • I faced the same problem one time, each time I change something in the dll, i create a new dll, and append a guid to its name. In this way i can add the dll to the appdomain and use the new dll updates. Please address exactly what is not clear for you in order for me to help you. Jan 17, 2011 at 10:05
  • thanks Ghyath Serhal for your answers . but it dosent work. because my codes was wroten deppend on the name of a fist dll. I think I should search for another way.
    – Mahsa
    Jan 17, 2011 at 10:10
8

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

4

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

0
2

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

1

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.

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.