Resolved Can I call a DLL dynamically or must I add reference?

Program4Food

Active member
Joined
Nov 22, 2021
Messages
40
Programming Experience
10+
Please keep in mind my experience with DLL's is just beginning...


I understand if I want to include a DLL in say a winform app, I need to add a reference to the DLL.

However I came across a page detailing dynamically added a DLL using LOAD.

So my question is: Can I load a DLL within my app, or must I make reference?


Example I am looking at is located at:




There is one other question sort of related to the above question.

If I want to use a DLL in my C# app, must the DLL have been compiled by the same language? (I am presuming not)

What is the requirement for loading a DLL? I presume my app must be in the same version or newer from
which the DLL was built, and also the version of my used DotNet must be the same or newer than that which
the DLL was built. (?)



Thank you-
Andrew
 
Solution
Your link there above with calling a .NET assembly and using reflection to use the classes defined within. If you are willing to go through all that because you want the flexibility (because you are trying to provide some kind of add-in functionality to your code), then go for it. The benefits of adding a reference is that you get a compile time error if there is something wrong with the way you are trying to access a class. When using reflection, you'll a runtime error instead.

All .NET languages gets compiled into IL (unless you force ahead-of-time compilation to native): C#, VB.NET, F#, IronPython, C++/CLI, etc.

Normally, you can only use assemblies compiled with the same version or older of the framework version that your...
Your link there above with calling a .NET assembly and using reflection to use the classes defined within. If you are willing to go through all that because you want the flexibility (because you are trying to provide some kind of add-in functionality to your code), then go for it. The benefits of adding a reference is that you get a compile time error if there is something wrong with the way you are trying to access a class. When using reflection, you'll a runtime error instead.

All .NET languages gets compiled into IL (unless you force ahead-of-time compilation to native): C#, VB.NET, F#, IronPython, C++/CLI, etc.

Normally, you can only use assemblies compiled with the same version or older of the framework version that your code is currently running in. See the exceptions that LoadFrom() could throw.

As an aside, you should use Load() instead of LoadFrom().

There is also the classical dynamically loading a DLL compiled in another language that has C or Pascal (aka WinAPI) compatible DLL exports. For that you'll want to use P/Invoke.
 
Last edited:
Solution
Back
Top Bottom