Best way to only perform a function if a (.NET) DLL is loaded?

Solution 1:

I do not think many people would actually use its functionality, so, I would rather not include it by default - just as an optional download.

Such things are typically described as plugins (or add-ons, or extensions).

Since .NET 4, the standard way to do that is with the Managed Exensibility Framework. It is included in the framework as the System.ComponentModel.Composition assembly and namespace. To get started, it is best to read the MSDN article and the MEF programming guide.

Solution 2:

You can use System.Reflection.Assembly and its LoadFile method to dynamically load a DLL. You can then use the methods in Assembly to get Classes, types etc. embedded in the DLL and call them.

If you just check if the .dll exists or load every .dll in a plugin directory you can get what you want.

Solution 3:

To your question if the program will run on the user's machine without the dlls already being present - yes , the program would run. As long as you dont do something that needs the runtime to load the classes defined in the dll , it does not matter if the dll is missing from the machine. To the aspect you are looking for regarding loading the dll on demand , I think you are well of using some sort of a configuration and Reflection ( either directly or by some IoC strategy. )