简体   繁体   English

C# - 在.NET Core上的Visual Studio 2015中的F#Interop支持

[英]C# - F# Interop support in Visual Studio 2015 on .NET Core

Note: here I talk about dotnet core and not full framework! 注意:这里我讲的是dotnet核心,而不是完整的框架! For full framework there are plenty of docs on this, but this is dotnet core. 对于完整的框架,有很多关于此的文档,但这是dotnet核心。

I have an F# library created by 我有一个F#库

dotnet new --lang F#

And an ASP.NET Core RC2 C# application (created by File->New Project in VS). 和一个ASP.NET Core RC2 C#应用程序(由VS中的File-> New Project创建)。

Now I use my F# library from C# (I guess a very common scenario…). 现在我使用来自C#的F#库(我猜这是一个非常常见的场景......)。 The F# project is referenced in the project.json as any other .net library (by listing it under dependencies) 在Project.json中引用F#项目作为任何其他.net库(通过在依赖项下列出它)

Now all this compiles and works fine, which is great! 现在所有这些编译和工作正常,这是伟大的!

The only problem is that VS does not seem to support it. 唯一的问题是VS似乎不支持它。 There is no intellisense and lines, where I use the F# functions from C# are marked as error. 没有intellisense和行,我使用C#中的F#函数标记为错误。 Debugging from C# to F# does not work either. 从C#到F#的调试也不起作用。 I tried it on a Mac with VS Code, same there… 我在带有VS Code的Mac上尝试过,同样的......

When I hit compile, the compilers figure this out and everything is fine. 当我点击编译时,编译器会想出来,一切都很好。

This screenshot summarizes it (this is where I call an F# function from C#): 这个截图总结了它(这是我从C#调用F#函数的地方): 在此输入图像描述

So basically I ended up with VS solution with a bunch of errors in it, which still compiles. 所以基本上我最终得到了VS解决方案,其中包含一堆错误,仍然可以编译。

Here is a simple reproducer. 是一个简单的复制器。

Questions: 问题:

  1. Should intellisense and debugging work in VS at all? 应该智能感知和调试在VS中工作吗?
  2. If yes, what did I do wrong? 如果是的话,我做错了什么?
  3. If no, is this scenario planned to be covered in the future? 如果不是,这个方案是否计划在未来涵盖?

Here is the code from the reproducer: 以下是重现器的代码:

F# library - project.json (created by cli - dotnet new --lang F# ) F#library - project.json(由cli创建 - dotnet new --lang F#

  "version": "1.0.0-*",
  "buildOptions": {
    "compilerName": "fsc",
    "compile": {
      "includeFiles": [
        "FSharpLibrary.fs"
      ]
    }
  },
  "dependencies": {
    "Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160316",
    "NETStandard.Library": "1.5.0-rc2-24027"
  },
  "tools": {
    "dotnet-compile-fsc": {
      "version": "1.0.0-*",
      "imports": [
        "dnxcore50",
        "portable-net45+win81",
        "netstandard1.3"
      ]
    }
  },
  "frameworks": {
    "netstandard1.5": {
      "imports": [
        "portable-net45+win8",
        "dnxcore50"
      ]
    }
  }

F# library, the code: F#库,代码:

namespace FSharpLibrary

module Sample =

    let public FSharpFunction number = 
            printfn "Hello From F#, %d" number

C# application (here I use a console app, but same things as with asp.net core) C#应用程序(这里我使用的是控制台应用程序,但与asp.net核心相同)

using System;
namespace ConsoleAppCSharp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            FSharpLibrary.Sample.FSharpFunction(42); //<- This is red. and marked as error.     
            Console.ReadKey();
        }
    }
}

C# console application project.json C#console应用程序project.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "LibraryFSharp": "1.0.0-*",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

Thanks 谢谢

So currently what I described above is all the support you have in Visual Studio 2015 with the preview tooling. 因此,目前我所描述的是您在Visual Studio 2015中使用预览工具获得的所有支持。 Visual Studio 15 will be hopefully better, but the current Preview (Preview 5) does not have .NET Core support at all . Visual Studio 15希望更好,但是当前预览版(预览版5) 根本没有.NET Core支持

What is worth trying out is Visual Studio Code. 值得一试的是Visual Studio Code。 In F# you get intellisense for type which are defined in C#. 在F#中,您可以获得在C#中定义的类型的智能感知。 So for F#-C# interopability currently VS Code has much better support than the full VS. 因此对于F#-C#互操作性,目前VS Code比完整的VS有更好的支持。

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

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