简体   繁体   English

有没有办法从 C# 程序运行 F# 代码?

[英]Is there a way to run F# code from a C# program?

I use Rider and/or Visual Studio for Mac to run my C# code.我使用 Rider 和/或 Visual Studio for Mac 来运行我的 C# 代码。 I can individually create an F# project if needed.如果需要,我可以单独创建一个 F# 项目。 How do I run the F# code within the C# main program:如何在 C# 主程序中运行 F# 代码:

class MainClass
{
    public static unsafe void Main(string[] args)
    {
        //F() = function that executes F# code
        Console.WriteLine(F());
    }
}

And F# code: printf("Hello, World!\n") or any code really和 F# 代码: printf("Hello, World!\n")或任何代码

I tried this in the terminal: Terminal VS error我在终端试过这个: Terminal VS error

Create an F# class library, add your F# code.创建一个 F# class 库,添加你的 F# 代码。 Create a C# console (or whatever) app, reference the F# class library, and call the f# code from your C# code.创建一个 C# 控制台(或其他)应用程序,引用 F# class 库,并从您的 C# 代码中调用 f# 代码。 you may need a 'using' statement to get your F# code accessible.您可能需要“使用”语句才能访问您的 F# 代码。

C#/F# code both compile to similar binary files (PE code, or something like that), which are mostly accessible from the other language, there are some features of both languages that don't translate (SRTPs being the most obvious thing not accessible from C#). C#/F# 代码都编译成类似的二进制文件(PE 代码,或类似的东西),这些文件大多可以从其他语言访问,两种语言的某些功能无法翻译(SRTP 是最明显的不可访问的东西来自 C#)。

Interoperability though is at this binary level, you can't just write F# code in a C# source file or vice versa (though this is possible in some other languages).尽管互操作性处于此二进制级别,但您不能只在 C# 源文件中编写 F# 代码,反之亦然(尽管这在某些其他语言中是可能的)。

instructions:指示:

create F# class library called "ClassLibrary1"创建 F# class 名为“ClassLibrary1”的库

in Visual Studio在视觉工作室

namespace ClassLibrary1

module Say =
    let hello name =
        printfn "Hello %s" name

ADD C# console app called "ConsoleApp1" to the same solution将名为“ConsoleApp1”的C#控制台应用程序添加到同一解决方案

Add project reference from consoleapp1 to classlibrary1将项目引用从 consoleapp1 添加到 classlibrary1

your C# project file should look something like this (obviously it may be slightly different but the crucial bit is the project reference)您的 C# 项目文件应该看起来像这样(显然它可能略有不同,但关键是项目参考)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.fsproj" />
  </ItemGroup>
</Project>

build建造

paste this code into your C# code将此代码粘贴到您的 C# 代码中

using ClassLibrary1;

Say.hello("Mr D");

If you can't get it to work do this如果你不能让它工作,那就这样做

https://learn.microsoft.com/en-us/do.net/core/tutorials/library-with-visual-studio?pivots=do.net-7-0 https://learn.microsoft.com/en-us/do.net/core/tutorials/library-with-visual-studio?pivots=do.net-7-0

get that working, then just do the first bit with an F# class library instead of a C# one让它工作,然后用 F# class 库而不是 C# 库做第一位

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

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