Question

I wrote an application in C# and added a kind of API for it. With this API you can write plugins as dll´s which underlie some interface rules.

I want to make it possible to open the dll file via OpenFileDialog and use its content. My API is a managed library, so I just add a reference, but I want to use the dll without knowing the name of the dll file. Also the namespace is another each library.

How do I load a dll and run the code within it?

Was it helpful?

Solution

What you are describing is commonly termed a Plugin System. Googling for something like "Create Plugin system using C#" will probably give you lots of information such as the below:

http://www.codeproject.com/Articles/4691/Plugin-Architecture-using-C

The basic idea is:

  • Define an interface that your program implements to allow a plugin to get information from your program.
  • Define an interface that all plugins will implement, to allow your program to call the plugin's methods that will do something.
  • Put those interfaces in a separate dll that's referenced by your program and by any plugin dlls.
  • Provide some way of finding dlls with types implementing your plugin interface, e.g. your OpenFileDialog.
  • Load the dll and find types that implement your plugin interface (using reflection).
  • Instanciate those types using reflection.
  • Call the methods on those types via the interface, as appropriate.

Regarding managed/non-managed. A managed DLL is one that is built/coded using the .net managed runtime. This would be things coded in a .net language such as .

A non-managed dll is more or less anything coded in a different language.

What you referred to as a non-managed dll I would refer to as a dynamically loaded managed dll. I.e. it's still a managed dll (coded in a .net language), but isn't loaded until the program is already running.

OTHER TIPS

You can load a managed assembly from a dll file with Assembly.LoadFrom Method (String) (See also Best Practices for Assembly Loading).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top