Produce a .NET 4.0 library from a PCL Project where code is identical

懵懂的女人 提交于 2019-12-04 13:12:26

With minimal conditional adjustments to the .csproj, an msbuild project can be created to compile a portable library solution to produce additional .net 4.0 profile binaries.

Build.proj:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup> 
        <ProjectToBuild Include="MyPortableSolution.sln">
            <Properties>Configuration=Release;</Properties>
        </ProjectToBuild>
        <ProjectToBuild Include="MyPortableSolution.sln">
            <Properties>Configuration=Release;OutputPath=bin\Release.net40\;IntermediateOutputPath=obj\Release.net40\;UseNet40=true;</Properties>
        </ProjectToBuild>
    </ItemGroup>
    <Target Name="Build">
        <MSBuild Projects="@(ProjectToBuild)"/>
    </Target>
</Project>

Make changes in two areas of MyPortableProj.csproj:

First Replace:

<TargetFrameworkProfile>Profile46</TargetFrameworkProfile>

With:

<UseNet40 Condition=" '$(UseNet40)' == '' ">false</UseNet40>
<TargetFrameworkProfile Condition="$(UseNet40) == false">Profile46</TargetFrameworkProfile>

Second Replace:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />

With:

<Import Condition="$(UseNet40) == true" Project="$(SolutionDir)\refs.targets" />
<Import Condition="$(UseNet40) == true" Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Condition="$(UseNet40) == false" Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />

Include a refs.targets

This is a file with the assembly references you need (edit from project to project):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Reference Include="mscorlib" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.Core" />
  </ItemGroup>
</Project>

note: using a separate file because Visual studio parses and displays it otherwise.

msbuild Build.proj

This will create your portable lib in bin\Release and .net 40 specific lib in bin\Release.net40

I guess there isn't a 'one size fits all' answer to this problem.

As a first step, it might be useful to try the pcl analyser in order to see how many types and methods fall outside of a 4.0 profile

  • if it's only a few methods then you might be able to find a way forwards using a lower profile pcl alongside a small number of platform specific abstractions.
  • but if there are a lot of incompatibilities then you may need to find some file-linking or project file transforming solution.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!