Reputation: 269
i'm fairly new to C# / .NET so I hope there is a solution for my problem.
I'm working with 2 different image-processing libraries which have their own classes to represent images. Because I don't want to use this classes in my own (private) class library, I want to implement my own "myImage"-Class which is supposed to be a wrapper around both of the other classes. (I have of course to add the functionality of the libraries if they are missing).
My problem is basically now like this: The myImage-class needs a reference to both libraries to compile, however i sometimes would like to use the myImage-class in projects where only one library is available. I find it not difficult to "add" or "remove" references to my image class project, however i don't want to comment out the code which refers to the missing library.
So my question is: is it somehow possible to determine wether a reference is set with a preprocessing directive or with attributes or with code directly? Or is there even a better solution for my problem (a clever class design maybe?)
Thanks for the help
Upvotes: 3
Views: 1307
Reputation: 2996
I don't think there's a preprocessor directive that will tell you if a reference has been added.
You could always split your class into two separate classes, one for each library, but I suspect your intention was to join the two into one unified class.
What I would do is use 2 conditional compilation symbols (see Properties | Build), one for methods specific to the first library, the other for the second library. Whenever the class's code refers to data members or functions specific the the library, it would be surrounded by a #if directive, referring to the appropriate symbol.
If your intention was not to join the classes, Reed's and Daniel's solutions are probably more appropriate.
Upvotes: 1
Reputation: 174457
In my answer, I am assuming, that both 3rd party image libraries provide the same functionality in regards to the features they expose, and you want to have them both, because one library performs one feature better, the other library performs the other feature better.
Having said that, you could create an interface that exposes the functionality, both libraries provide.
You put that interface in an interface assembly.
You create two additional assemblies. In each of those, you put an implementation of your interface, which wraps one specific 3rd party library.
Now, when you want to use some image processing functionality, you use that interface and pass in that concrete instance of the 3rd party library, you want to use.
If that is not applicable in your case, please explain a bit in more detail, why you want to encapsulate two libraries.
Upvotes: 1
Reputation: 564801
A better option would be to have your wrapper define an interface. You could then use two separate assemblies (libraries) for each of the image processing libraries, which could implement the interface.
At runtime, you could use configuration or a tool like MEF to automatically load the available library into your wrapper API, and use it via the interface.
This allows you to completely isolate the 2 libraries, as well as provides the flexibility to extend this with other versions/libraries in the future.
Upvotes: 1