配置使用TargetFrameworks输出多版本类库
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>