问题
I have a project which contains two F# projects and a C# project in which I'd like to write some XUnit tests:
- FS_PL: F# 3.1 (3.3.1.0) Portable Library
- FS_PL_Legacy: F# 31. (2.3.5.1) Portable Library (Legacy)
- Tests: C# .NET 4.5/Win8 C# Portable Class Library (PCL)
I am unable to add a reference from Tests to either of the F# libraries.
When I try to add a reference to FS_PL, I am presented with a dialog that states "Unable to add a reference to project 'FS_PL'. The targets of Portable Library project 'FS_PL' are not the same or compatible with the targets of the current Portable Library project":
This is odd since both my Tests and FS_PL libraries are configured to target .NET 4.5 & Windows 8.
So I created FS_PL_Legacy and tried adding a reference to it. Doing so gives me a very 'helpful' message stating "Unable to add a reference to project 'FS_PL_Legacy'":
Does anyone know what I am doing wrong?
Partial Workaround #1
Using miegirl's workaround from a Connect issue discussing this problem, I added the following to the C# project:
<ItemGroup>
<!-- Manually added reference to F# projects to overcome issue discussed here:
http://stackoverflow.com/questions/23111782/how-do-i-add-a-reference-to-f-portable-library-from-c-sharp-portable-class-libr
-->
<ProjectReference Include="..\FS_PL\FS_PL.fsproj">
<Project>{2c4b1776-3d34-4534-8520-8a1e6daa0e6e}</Project>
<Name>FS_PL</Name>
</ProjectReference>
<ProjectReference Include="..\FS_PL_Legacy\FS_PL_Legacy.fsproj">
<Project>{0d7b657c-906b-4448-ae64-2153a1fa910c}</Project>
<Name>FS_PL_Legacy</Name>
</ProjectReference>
</ItemGroup>
This at least allows VS to reference the F# projects, but those projects are tagged with several warnings and the C# portable library is unable to build as it cannot reference the types in one or both of the F# libraries :(
Most of the warnings in the build output window indicate that the F# libraries cannot be referenced as they appear to have "an indirect dependency on the framework assembly "[System.Threading/System.Lync/etc.]" which could not be resolved in the currently targeted framework"
回答1:
Ask and ye shall be heard ...
I raised this issue on Twitter with Don Syme and some of the F# team. They confirmed that it was an issue they had a fix that was working its way through the release process.
I installed today's Visual F# daily build and I can confirm that this issue has been resolved!
Thanks to Don and the F# team for fixing this issue - I can now incorporate new F# code into my predominantly C# codebase & test suite ... and begin replacing chunks of our system with FAR less F# code than the equivalent C# ;)
回答2:
This bug is old, but still occurs. I ran into this issue when running VS 2015 Community. I realize that this question is about VS 2013, but I was not sure else where to post my answer.
I had a Xamarin Forms Xaml app and I wanted to add an F# PCL to do some calculations. I saw the warning message when trying to reference the F# PCL in my C# PCL:
Unable to add a reference to 'PortableLibrary1'. Portable Library projects can only reference other Portable Library projects and assemblies.
I tried editing the csproj file directly and even tried upgrading to VS 2017 RC but nothing was working.
The fix is a bit of a hack.
- First build everything (including the Portable F# project)
- Add the F# reference to the C# project by browsing to the output DLL. i.e. Right Click on CS Project, Add -> Reference... -> Browse -> Click "Browse..." button and select the F# project DLL (mine was in my debug folder), then click OK.
- The F# PCL now shows in the references list in solution explorer.
- Add the F# project reference to the C# project again but by selecting the project i.e. Right Click on CS Project, Add -> Reference... -> Projects -> Check the PortableLibrary project and click OK.
- This gives me no error message box about adding the reference, and I can get to all my F# goodness in my C# project.
回答3:
I don't know the root cause, but you can manually edit the .csproj file to add a reference to the F# project. It will show a warning in the solution explorer indicating that something's still wrong, but it seems to work in practice.
来源:https://stackoverflow.com/questions/23111782/how-do-i-add-a-reference-to-f-portable-library-from-c-sharp-portable-class-libr