简体   繁体   English

如何从C#可移植类库(PCL)添加对F#可移植库的引用

[英]How do I add a reference to F# Portable Library from C# Portable Class Library (PCL)

I have a project which contains two F# projects and a C# project in which I'd like to write some XUnit tests: 我有一个项目,其中包含两个F#项目和一个C#项目,我想在其中编写一些XUnit测试:

  • FS_PL: F# 3.1 (3.3.1.0) Portable Library FS_PL:F#3.1(3.3.1.0)可移植库
  • FS_PL_Legacy: F# 31. (2.3.5.1) Portable Library (Legacy) FS_PL_Legacy:F#31。(2.3.5.1)便携式库(旧版)
  • Tests: C# .NET 4.5/Win8 C# Portable Class Library (PCL) 测试:C#.NET 4.5 / Win8 C#可移植类库(PCL)

I am unable to add a reference from Tests to either of the F# libraries. 我无法将测试中的引用添加到任何一个F#库中。

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": 当我尝试添加对FS_PL的引用时,我看到一个对话框,指出“无法添加对项目'FS_PL'的引用。可移植库项目'FS_PL'的目标与目标不同或与当前便携式图书馆项目“: 在此输入图像描述

This is odd since both my Tests and FS_PL libraries are configured to target .NET 4.5 & Windows 8. 这很奇怪,因为我的测试和FS_PL库都配置为以.NET 4.5和Windows 8为目标。

So I created FS_PL_Legacy and tried adding a reference to it. 所以我创建了FS_PL_Legacy并尝试添加对它的引用。 Doing so gives me a very 'helpful' message stating "Unable to add a reference to project 'FS_PL_Legacy'": 这样做给了我一个非常“有用”的消息,指出“无法添加对项目'FS_PL_Legacy'的引用”: 在此输入图像描述

Does anyone know what I am doing wrong? 有谁知道我做错了什么?

Partial Workaround #1 部分解决方法#1

Using miegirl's workaround from a Connect issue discussing this problem , I added the following to the C# project: 使用miegirl解决此问题的Connect问题的解决方法,我在C#项目中添加了以下内容:

<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 :( 这至少允许VS引用F#项目,但是这些项目被标记了几个警告并且C#可移植库无法构建,因为它无法引用一个或两个F#库中的类型:(

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" 构建输出窗口中的大多数警告表明无法引用F#库,因为它们似乎具有“对框架程序集的间接依赖”[System.Threading / System.Lync / etc.]“无法在目前有针对性的框架“

Ask and ye shall be heard ... 问,你们会被听到......

I raised this issue on Twitter with Don Syme and some of the F# team. 在Twitter上与Don Syme和一些F#团队提出了这个问题 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! 我安装了今天的Visual F#daily build ,我可以确认这个问题已经解决了!

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# ;) 感谢Don和F#团队解决这个问题 - 我现在可以将新的F#代码整合到我主要的C#代码库和测试套件中......并开始使用FAR减去F#代码而不是等效的C#代替我们系统的块;)

This bug is old, but still occurs. 这个bug很旧,但仍然会发生。 I ran into this issue when running VS 2015 Community. 我在运行VS 2015社区时遇到了这个问题。 I realize that this question is about VS 2013, but I was not sure else where to post my answer. 我意识到这个问题是关于VS 2013的,但我不确定在哪里发布我的答案。

I had a Xamarin Forms Xaml app and I wanted to add an F# PCL to do some calculations. 我有一个Xamarin Forms Xaml应用程序,我想添加一个F#PCL来做一些计算。 I saw the warning message when trying to reference the F# PCL in my C# PCL: 我在C#PCL中尝试引用F#PCL时看到了警告信息:

Unable to add a reference to 'PortableLibrary1'. 无法添加对'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. 我尝试直接编辑csproj文件,甚至尝试升级到VS 2017 RC,但没有任何工作。

The fix is a bit of a hack. 修复是一个黑客攻击。

  1. First build everything (including the Portable F# project) 首先构建一切(包括Portable F#项目)
  2. Add the F# reference to the C# project by browsing to the output DLL. 通过浏览到输出DLL将F#引用添加到C#项目。 ie 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. 即右键单击CS项目,添加 - >参考... - >浏览 - >单击“浏览...”按钮并选择F#项目DLL(我的调试文件夹中有我的),然后单击“确定”。
  3. The F# PCL now shows in the references list in solution explorer. F#PCL现在显示在解决方案资源管理器的参考列表中。
  4. Add the F# project reference to the C# project again but by selecting the project ie Right Click on CS Project, Add -> Reference... -> Projects -> Check the PortableLibrary project and click OK. 再次将F#项目引用添加到C#项目但选择项目即右键单击CS项目,添加 - >引用... - >项目 - >检查PortableLibrary项目,然后单击确定。
  5. This gives me no error message box about adding the reference, and I can get to all my F# goodness in my C# project. 这给了我关于添加引用的错误消息框,我可以在我的C#项目中获得我所有的F#优点。

I don't know the root cause, but you can manually edit the .csproj file to add a reference to the F# project. 我不知道根本原因,但您可以手动编辑.csproj文件以添加对F#项目的引用。 It will show a warning in the solution explorer indicating that something's still wrong, but it seems to work in practice. 它将在解决方案资源管理器中显示一个警告,表明某些内容仍然存在问题,但它似乎在实践中有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM