Conditional compilation depending on the framework version in C#
Asked Answered
H

5

25

Are there any preprocessor symbols which allow something like

#if CLR_AT_LEAST_3.5
// use ReaderWriterLockSlim
#else
// use ReaderWriterLock
#endif

or some other way to do this?

Hughmanick answered 3/1, 2009 at 11:3 Comment(1)
Note that IIRC this only applies to regular 3.5, not CF 3.5 - you might want to name any symbols accordingly.Mezzanine
S
21

I don't think there are any predefined 'preprocessor' symbols. However you can achieve what you want like this:

  1. Create different configurations of your project, one for every version of CLR you want to support.

  2. Choose a symbol like VERSION2, VERSION3 etc. per CLR version.

  3. In every configuration, define the one symbol associated with it and undefine all others.

  4. Use these symbols in conditional compilation blocks.

Stripy answered 3/1, 2009 at 11:10 Comment(0)
M
6

There aren't any built in, but you can supply your own.

For this specific scenario, you might want to encapsulate the logic in (for example) a wrapper (lock) class, so that you don't have #if scattered through all the code; of course, if you are only doing a little locking it might not be worth the trouble.

I use different configurations and/or projects to build for a variety of platforms - i.e. protobuf-net builds for .NET 2.0, .NET 3.0, mono, CF 2.0, CF 3.5 using this trick. The code has #if blocks based on different symbols to control logic - so, for example, BinaryFormatter isn't available on CF, WCF is only available with .NET 3.0, Delegate.CreateDelegate isn't on CF 2.0, etc.

Mezzanine answered 3/1, 2009 at 11:24 Comment(0)
L
4

You could use reflection to check dynamically whether a certain type like ReaderWriterLockSlim is available (instead of using the preprocessor).

This would give you the advantage that you can deploy a single version of your product and users having (or updating to) .NET 3.5 will benefit from the optimized code.

Lyall answered 3/1, 2009 at 11:13 Comment(3)
But it would make using it a complete pig... doable, but not very attractive. In particular, some of the .NET 3.5 features (like Expression) would be very hard to use via reflection.Mezzanine
Marc, I would not use reflection throughout the code, just to check whether a certain library / type is available on the client machine. This would be very similar to the preprocessor symbols (i.e. an if condition and then branching depending whether 3.5 is there or not)Lyall
Obviously you would need to cache any reflection results if you go this route.Hughmanick
B
2

You could manually set this symbol using the /define compiler switch. Then you create different build configurations for each desired clr version.

Bodkin answered 3/1, 2009 at 11:10 Comment(0)
M
1

If that's all you needed to do, I suppose you could use Environment.Version, but like divo's solution, it does seem to leave a lot of unneccessary code there.

Mooneyham answered 4/1, 2009 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.