Dll References and Dependencies
-
Hi, I have the following issue with my WinForms C# project and wonder if someone could point me to a solution... I have a reference to a 3rd party DLL (which I am charged for and is licensed on a per end-user basis). The dll allows me to run certain functionality within my project. Ideally I'd like to to check if the Licence file \ DLL exists (I can do this) then if it exists, proceed as normal, but if it doesn't exists, then remove access to the functionality the Dll provides. The issue I have is that I have references in the project to the DLL, so if the licence file is not there or the DLL file is not present, the app explodes on startup. Is there any way I can get around this issue? Many thanks.
-
Hi, I have the following issue with my WinForms C# project and wonder if someone could point me to a solution... I have a reference to a 3rd party DLL (which I am charged for and is licensed on a per end-user basis). The dll allows me to run certain functionality within my project. Ideally I'd like to to check if the Licence file \ DLL exists (I can do this) then if it exists, proceed as normal, but if it doesn't exists, then remove access to the functionality the Dll provides. The issue I have is that I have references in the project to the DLL, so if the licence file is not there or the DLL file is not present, the app explodes on startup. Is there any way I can get around this issue? Many thanks.
Good question. I found this article: Loading an Assembly[^] If that doesn't solve the problem, you can dynamically load the assembly with the Assembly.Load[^] method, and then create objects from it with the Activator.CreateInstance[^] method. This will allow you to remove the static reference from your project. Also, see this article about early and late binding: Early and Late Binding[^] EDIT: I apologize, that article is about VB. But if you search on "Late Binding C#" you'll find good information.
The difficult we do right away... ...the impossible takes slightly longer.
-
Good question. I found this article: Loading an Assembly[^] If that doesn't solve the problem, you can dynamically load the assembly with the Assembly.Load[^] method, and then create objects from it with the Activator.CreateInstance[^] method. This will allow you to remove the static reference from your project. Also, see this article about early and late binding: Early and Late Binding[^] EDIT: I apologize, that article is about VB. But if you search on "Late Binding C#" you'll find good information.
The difficult we do right away... ...the impossible takes slightly longer.
-
Hi, I have the following issue with my WinForms C# project and wonder if someone could point me to a solution... I have a reference to a 3rd party DLL (which I am charged for and is licensed on a per end-user basis). The dll allows me to run certain functionality within my project. Ideally I'd like to to check if the Licence file \ DLL exists (I can do this) then if it exists, proceed as normal, but if it doesn't exists, then remove access to the functionality the Dll provides. The issue I have is that I have references in the project to the DLL, so if the licence file is not there or the DLL file is not present, the app explodes on startup. Is there any way I can get around this issue? Many thanks.
What I would do is have the functionality declared in an external assembly - the classes I wanted would implement interfaces stored in a DLL or the Exe and I would then use MEF to wire things together. This would allow you to use the interface as your type in the main program.
-
What I would do is have the functionality declared in an external assembly - the classes I wanted would implement interfaces stored in a DLL or the Exe and I would then use MEF to wire things together. This would allow you to use the interface as your type in the main program.
MEF? :confused:
The difficult we do right away... ...the impossible takes slightly longer.
-
MEF? :confused:
The difficult we do right away... ...the impossible takes slightly longer.
Managed Extensibility Framework[^] Wonderful magic!
-
Hi, I have the following issue with my WinForms C# project and wonder if someone could point me to a solution... I have a reference to a 3rd party DLL (which I am charged for and is licensed on a per end-user basis). The dll allows me to run certain functionality within my project. Ideally I'd like to to check if the Licence file \ DLL exists (I can do this) then if it exists, proceed as normal, but if it doesn't exists, then remove access to the functionality the Dll provides. The issue I have is that I have references in the project to the DLL, so if the licence file is not there or the DLL file is not present, the app explodes on startup. Is there any way I can get around this issue? Many thanks.
You can actually catch the exception that gets thrown when a class can't be loaded. The trick is that the class must not be used in your Main method, because entering a method which uses a class the point that the class binder tries to load it, and you can't catch it. So you can do something like
static void Main() {
try { EntryPoint(); }
catch(FileNotFoundException) {
// ...
// e.g. put up message that a dependency is missing
}
}static void EntryPoint() {
var x = new DependencyClass(); // declared in other assembly
}The 'correct' way to do it is to handle Application.AssemblyResolve but what I've described here can be useful if all you want to do is tell the user they need to install something which is missing in a more graceful way.
-
You can actually catch the exception that gets thrown when a class can't be loaded. The trick is that the class must not be used in your Main method, because entering a method which uses a class the point that the class binder tries to load it, and you can't catch it. So you can do something like
static void Main() {
try { EntryPoint(); }
catch(FileNotFoundException) {
// ...
// e.g. put up message that a dependency is missing
}
}static void EntryPoint() {
var x = new DependencyClass(); // declared in other assembly
}The 'correct' way to do it is to handle Application.AssemblyResolve but what I've described here can be useful if all you want to do is tell the user they need to install something which is missing in a more graceful way.