是否有可能有条件地编译到.NET Framework版本?有可能、有条件、版本、NET

2023-09-03 00:05:46 作者:G-棺材。

我还记得回来MFC工作时,你可以支持多个版本的MFC框架通过检查 _MFC_VER 宏。

我做的一些东西现在随着.NET 4中,并想用元组在几个点,但仍保留一切3.5兼容。

我希望做这样的事情:

 #如果DOTNET4
    公开元组LT; TSource,TResult>的someMethod&所述; TSource,TResult>(){...}
#其他
    公共KeyValuePair< TSource,TResult>的someMethod&所述; TSource,TResult>(){...}
#ENDIF
 

解决方案

有可以不使用内置precompiler常数。但是,这是很容易的创建VS自己的编译配置,每种配置有它自己的一套定义的常量,当然还有一个目标框架的版本。很多人这样做是为了有条件地编译基于32位或64位的差异。

vs2010创建工程后如何改变.net framework 版本

I can recall back when working with MFC you could support multiple versions of the MFC framework by checking the _MFC_VER macro.

I'm doing some stuff now with .NET 4 and would like to use Tuple in a couple of spots but still keep everything else 3.5 compatible.

I'm looking to do something like:

#if DOTNET4
    public Tuple<TSource, TResult> SomeMethod<TSource, TResult>(){...}
#else
    public KeyValuePair<TSource, TResult> SomeMethod<TSource, TResult>(){...}
#endif

解决方案

There are no builtin precompiler constants that you can use. But, it is easy enough create your own build configurations in VS with each configuration having its own set of defined constants and of course a target framework version. A lot of people do this to conditionally compile based 32 or 64 bit differences.