不同平台框架项目使用同一套代码,一次编译生成多个框架类库
[TOC]
需要先了解的东西
分析
- 使用.NET 可移植性分析器工具分析项目依赖
- .NET 可移植性分析器工具有关博客
- 根据分析结果以及建议,到这个网站可以根据fx框架下类型名查找对应的nuget包,来替代不兼容的引用
- 重新组织项目依赖,整理各个框架公共的代码文件和其他平台不支持的功能的代码文件,必要时可将一个文件分成多个多个文件
- 由于我这里已经是多个分开的项目,就省了分析代码这一步,直接分析文件应该归属哪些项目,哪些独有的
添加PropertyGroup
多目标平台
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net40;net45;netstandard2.0</TargetFrameworks>
</PropertyGroup>
</Project>
编译符号和输出目录设置
<PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Debug|AnyCPU|net45'">
<OutputPath>..\..\Bin\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DocumentationFile>..\..\Bin\net45\XCode.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Release|AnyCPU|net45'">
<OutputPath>..\..\Bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile>..\..\Bin\net45\XCode.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)'=='Debug|AnyCPU|netstandard2.0'">
<OutputPath>..\..\Bin\</OutputPath>
<DocumentationFile>..\..\Bin\netstandard2.0\XCode.xml</DocumentationFile>
<DefineConstants>TRACE;DEBUG;NETSTANDARD2_0;__CORE__</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)'=='Release|AnyCPU|netstandard2.0'">
<OutputPath>..\..\Bin\</OutputPath>
<DocumentationFile>..\..\Bin\netstandard2.0\XCode.xml</DocumentationFile>
<DefineConstants>TRACE;RELEASE;NETSTANDARD2_0;__CORE__</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Debug|AnyCPU|net40'">
<OutputPath>..\..\Bin\</OutputPath>
<DefineConstants>TRACE;DEBUG;NET4</DefineConstants>
<DocumentationFile>..\..\Bin\net40\XCode.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Release|AnyCPU|net40'">
<OutputPath>..\..\Bin\</OutputPath>
<DefineConstants>TRACE;NET4</DefineConstants>
<DocumentationFile>..\..\Bin\net40\XCode.xml</DocumentationFile>
</PropertyGroup>
添加依赖
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'netstandard2.0' ">
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Management" />
<Reference Include="System.Security" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Management" />
<Reference Include="System.Security" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NewLife.Core\NewLife.Core.csproj">
<Name>NewLife.Core</Name>
</ProjectReference>
</ItemGroup>
- 如果依赖项那里显示感叹号,关掉项目重新打开即可
代码文件处理
- 将前面分析好的代码文件分情况做排除,由于项目里面的文件自动显示在解决方案,不用显式包含文件,只需要根据目标平台做好排除即可
主副平台项目文件处理
全部平台都包含
- MSBuild项目文件会自动包含项目文件下面的所有文件,不必处理
一个或多个平台包含
- 在所有不包含的此文件的平台的ItemGroup中移除
没有平台包含
- 没有项目包含此文件为什么还有放在项目文件夹?我也不知道,在全局ItemGroup移除即可