1.類庫右鍵
2.修改配置
修改前:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> </Project>
修改后:
<PropertyGroup> <TargetFrameworks>netstandard2.0;net45</TargetFrameworks> </PropertyGroup>
3.依賴:
<ItemGroup> <Reference Include="System.Net" /> </ItemGroup>
這樣表示net40和netstand2.0都需要System.Net引用,
展開會看到在netstandard2.0上出現了感嘆號,表示netstandard2.0並不知道System.Net是什么東東
實際只有net40才需要該引用,所以這里我們要使用Condition
<ItemGroup Condition="'$(TargetFramework)' == 'net40'"> <Reference Include="System.Net" /> </ItemGroup>
這表示只有net40才符合條件,保存后你會發現依賴項那邊的感嘆號消失了
4.debugger
定義constants
<PropertyGroup Condition=" '$(TargetFramework)' == 'net40' "> <DefineConstants>NETFULL</DefineConstants> </PropertyGroup>
#if NETFULL int number; #else string number; #endif
5.擴展
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' OR '$(TargetFramework)' == 'net46' "> <Reference Include="System" /> <Reference Include="Microsoft.CSharp" /> </ItemGroup> <PropertyGroup Condition=" '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net46'"> <DefineConstants>$(DefineConstants);FEATURE_SERIALIZATION;FEATURE_SOCKET_MODE_POLL;FEATURE_PERFCOUNTER;FEATURE_THREADPOOL</DefineConstants> </PropertyGroup>