1. 程式人生 > 其它 >為.Net Framework工程新增編譯時檢測框架版本

為.Net Framework工程新增編譯時檢測框架版本

在新的.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')))) &gt;= 0">$(DefineConstants);NET48;NET48_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.7.2')))) &gt;= 0">$(DefineConstants);NET472;NET472_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.7.1')))) &gt;= 0">$(DefineConstants);NET471;NET471_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.7')))) &gt;= 0">$(DefineConstants);NET47;NET47_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.6.2')))) &gt;= 0">$(DefineConstants);NET462;NET462_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.6.1')))) &gt;= 0">$(DefineConstants);NET461;NET461_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.6')))) &gt;= 0">$(DefineConstants);NET46;NET46_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.2')))) &gt;= 0">$(DefineConstants);NET452;NET452_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.1')))) &gt;= 0">$(DefineConstants);NET451;NET451_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5')))) &gt;= 0">$(DefineConstants);NET45;NET45_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.0')))) &gt;= 0">$(DefineConstants);NET40;NET40_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.5')))) &gt;= 0">$(DefineConstants);NET35;NET35_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.0')))) &gt;= 0">$(DefineConstants);NET30;NET30_OR_GREATER</DefineConstants>
    <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('2.0')))) &gt;= 0">$(DefineConstants);NET20;NET20_OR_GREATER</DefineConstants>
  </PropertyGroup>
</Project>

參考

Detect target framework version at compile time
C#前處理器指令