How to use functions of a dll in another dll "on the fly"
-
Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.
-
Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.
That sounds the wrong way round to me. The base (not master) dll should not have, or even need, any knowledge of extensions. The point of an extension class is to offer extra or enhanced functionality to an application. So the only process that needs to know whether the extensions are present is the application that wants to use its features.
-
That sounds the wrong way round to me. The base (not master) dll should not have, or even need, any knowledge of extensions. The point of an extension class is to offer extra or enhanced functionality to an application. So the only process that needs to know whether the extensions are present is the application that wants to use its features.
-
The "Master" dll (or base dll) is the application in this case. It is a plugin dll for a software where i do not have the source cose and also extends the functionality of that software. So you could consider the base dll as the application.
-
Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.
Hi, I have an appliction that loads extensions from a specific folder; each extension is a class that derives from a base class
DTF_ComponentBase
, which happens to be aUserControl
as all extensions need a very similar dialog window. Each extension is built into a separate DLL file. This is the heart of the code that loads those DLL files:private readonly List DTFlist=new List(); ... // scan DTFAddins folder DTFlist.Clear(); string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); //folder += @"\\DTFAddins"; log("folder=" + folder); Directory.CreateDirectory(folder); string\[\] dlls = Directory.GetFiles(folder, "\*.dll"); Exception firstException = null; List errors = new List(); foreach (string dll in dlls) { if (dll.Contains("LPplatformDLL.dll")) continue; try { log("File: " + dll); if (dll.Contains("DTF\_ComponentBase.dll")) continue; Assembly asm2 = Assembly.LoadFile(dll); Type\[\] types = asm2.GetTypes(); // find all classes implementing DTF\_ComponentBase foreach (Type type in types) { //log(" Type=" + type.FullName); if (type.IsSubclassOf(typeof(UserControl))) { UserControl uc = (UserControl)Activator.CreateInstance(type); if (uc is DTF\_ComponentBase comp && !comp.Skip) { DTFlist.Add(comp); break; } } } } catch (Exception exc) { if (firstException == null) firstException = exc; errors.Add("Error loading "+dll+"; "+exc.Message); } }
At the end
DTFlist
contains all the extensions that were found and instantiated; a foreach loop can then be used to issue commands such as Start, Stop, ... whatever the base class provides. Hope this helps. :)Luc Pattyn [My Articles] The Windows 11 taskbar is a disgrace; a third-party add-on is needed to reverse the deterioration. I decline such a downgrade.
-
Hello, I have a VS project to implement a dll. This dll is a kind of "Master" dll and it should be possible to extend its functionality with "Extension" dlls without touching the "Master" dll. The "Master" dll should check everytime when it is loaded, what other dlls are available and thus extend or reduce its functionality by itself. If "Extension" dlls are missing, the "Master" dll should recognize this without any error and function with reduced functionality. Any hints from your side which approach would make sense here.
Presumably you have a well designed understanding of what the additional functionality is in general. There are a number of design patterns that might be useful in structuring the code. - Chain of responsibility - Fly weight - Decorator - Composite - Template As suggested other response you use reflection to load classes dynamically. Those will need either an interface or base class for your code to interact with it. You will need to at least consider dependencies (other dlls required by the dll that is loaded) in that they must be located somewhere. Either load those also dynamically or insure that the application can find them.
AtaChris wrote:
with reduced functionality
That is a design and business consideration which cannot be addressed generically. For example perhaps you want to switch out your database driver. But the application cannot operate without any database. Same thing with supporting multiple card card processor interfaces. If you expect two but only find one then that is ok. But if you find none then the application probably cannot continue. There are ways around failure cases but they add significant complexity and even business risk.