I have a project which runs fine on 4.0, however in the interests of utilizing the new ZipFile class I have decided to target 4.5. The vast majority of my users will have 4.5+ installed, but its not impossible someone with only 4.0 may come along and try to use my software.
Since it runs perfectly fine on 4.0, with the exception of the ZipFile class, can I target 4.5, and if 4.0 is detected it simply disables this particular codepath and continue to run normally in all other aspects?
I assume however I can't make a backwards-compatibility mode this easily (at all?), due to having had to reference a 4.5 assembly to utilize ZipFile in the first place.
On a side note, I have already implemented detection of what framework version is installed. I can quite easily disable this section of code if <4.5 is detected.
Best How To :
The easiest way to do this is probably to load the required code dynamically.
You could use reflection to look for the required assembly, class and members if .NET 4.5 is detected. You could cache the results in delegates, to avoid reflecting every time.
You could also use a plug-in system where your plug-in that adds zip file support targets .NET 4.5, but your main application only targets .NET 4.0. Your main application then determines whether it can load the plug-in.
But what you suggested, targeting .NET 4.5 and simply avoiding .NET 4.0 features, is something I would very strongly discourage. You don't know which .NET 4.5 features will be used implicitly, if .NET 4.5 is targeted. This breaks many NuGet packages, which will wrongly install the .NET 4.5 version when you need the 4.0 version. This breaks
async. This probably breaks much more.