為.Net Framework工程新增編譯時檢測框架版本
阿新 • • 發佈:2022-04-15
在新的.NET框架下,官方自帶了一些用於判斷框架版本的條件變數(C#前處理器指令)
符號檔案
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.8')))) >= 0">$(DefineConstants);NET48;NET48_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.7.2')))) >= 0">$(DefineConstants);NET472;NET472_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.7.1')))) >= 0">$(DefineConstants);NET471;NET471_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.7')))) >= 0">$(DefineConstants);NET47;NET47_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.6.2')))) >= 0">$(DefineConstants);NET462;NET462_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.6.1')))) >= 0">$(DefineConstants);NET461;NET461_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.6')))) >= 0">$(DefineConstants);NET46;NET46_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.2')))) >= 0">$(DefineConstants);NET452;NET452_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.1')))) >= 0">$(DefineConstants);NET451;NET451_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5')))) >= 0">$(DefineConstants);NET45;NET45_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.0')))) >= 0">$(DefineConstants);NET40;NET40_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.5')))) >= 0">$(DefineConstants);NET35;NET35_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.0')))) >= 0">$(DefineConstants);NET30;NET30_OR_GREATER</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('2.0')))) >= 0">$(DefineConstants);NET20;NET20_OR_GREATER</DefineConstants> </PropertyGroup> </Project>