conditionally declare (DllImport) libraries at runtime
Posted on 2006-04-01
I have an application with several classes that make use of libraries that use P/Invoke (i.e. they are declared with the [DllImport] attribute).
There are three different libraries (with largely identically defined functions) that I need to import from, depending on different circumstances. For any given installation (or at least at runtime), I only need one of the three.
What I'd like to do is set up an App.Config setting that would allow me to specify which library will be imported. My question is whether and how is it possible to conditionally declare such external platform libraries ... without having to have three sets of classes that are indentical except for their library import statements.
It do not see a straight-forward way to apply any conditional logic to the declaration. If there were such an opportunity, that would be ideal. But, I'm thinking that there may be a way to accomplish something close to this outcome with class inheritance. Is it possible to do something like create a base class which calls functions which are declared in the classes that inherit from it? Perhaps this is overcomplicating the problem?
At any rate, I'd appreciate specific advice on how to achieve the goal of conditionally importing libraries. Thanks.