How to handle dynamically loaded managed libraries in C #

I wrote an application in C # and added some kind of API for it. With this API, you can write plugins as dlls that underlie some interface rules.

I want to open a dll file through OpenFileDialog and use its contents. My API is a managed library, so I just add the link, but I want to use dll without knowing the dll file name. In addition, the namespace is another library.

How to load a DLL and run the code inside it?

+4
source share
2 answers

What you describe is usually called a plugin system. Googling for something like "Create Plugin system using C #" will probably give you a lot of information, like below:

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

Main idea:

  • Define the interface that your program implements so that the plugin can receive information from your program.
  • Define the interface that all plugins will implement so that your program calls plugin methods that will do something.
  • Put these interfaces in a separate dll, which is referenced by your program and any plug-ins.
  • Provide some way to search for dlls with types that implement your plugin interface, for example. your OpenFileDialog.
  • Download the dll and find the types that implement your plugin interface (using reflection).
  • Import these types using reflection.
  • Call methods on these types through the interface, if necessary.

Relatively managed / non-managed. A managed DLL is one that is built / encoded using a managed .net environment. These will be things encoded in the .net language, for example .

An unmanaged dll is more or less encoded in another language.

What you would call an unmanaged dll, I would call a dynamically loaded managed dll. That is, it is still a managed dll (encoded in .net language), but is not loaded until the program is launched.

+4
source

You can load a managed assembly from a DLL file using the Assembly.LoadFrom Method (String) (See also Assembly Recommendations ).

+1
source

Source: https://habr.com/ru/post/1435951/


All Articles