Conditional compilation depending on the framework version in C#

前端 未结 5 1359
無奈伤痛
無奈伤痛 2020-11-27 06:08

Are there any preprocessor symbols which allow something like

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


        
5条回答
  •  死守一世寂寞
    2020-11-27 06:52

    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.

提交回复
热议问题