是否可以将.cs (c#)和.fs (f#)文件混合到一个Visual Studio Windows控制台项目中?(. net)

时间:2021-03-21 22:44:05

How to do it? Is it possible to call a function from one F# code into C# without using a separated dll file or project?

如何去做?是否可以在不使用分离的dll文件或项目的情况下,将一个函数从一个f#代码调用到c#中?

5 个解决方案

#1


21  

You can't include two different languages in the same project, but you can merge them using ilmerge. To do this, put both projects in the same solution and reference the F# module as you would any dll. As part of your deployment script, run ilmerge to combine the exe file and dll file into a single exe file. See this Code Project article that details how to use ilmerge to create an exe.

不能在同一个项目中包含两种不同的语言,但是可以使用ilmerge来合并它们。为此,将两个项目放在同一个解决方案中,并像引用任何dll一样引用f#模块。作为部署脚本的一部分,运行ilmerge,将exe文件和dll文件合并到一个exe文件中。请参阅这篇关于如何使用ilmerge创建exe的代码项目文章。

#2


15  

Not being able to mix C# and F# in the same project creates a problem; It leads to circular dependencies between the two projects. Conceptually there is only one project containing two languages, but because of the way visual studio manages projects languages cannot be mixed, leading to circular dependencies.

不能在同一个项目中混合c#和f#会产生问题;它导致两个项目之间的循环依赖。从概念上讲,只有一个项目包含两种语言,但是由于visual studio管理项目语言的方式不能混合,导致循环依赖。

For now the only solution I see it to create a lot of interfaces in a third project and have one of the project refer to the interfaces project, instead of the real project. Is there a better way?

目前,我看到的唯一解决方案是在第三个项目中创建大量接口,并让其中一个项目引用接口项目,而不是真正的项目。有更好的方法吗?

Kind Regards, Nick

亲切的问候,尼克

#3


12  

No. If you want to produce a single .exe you could use some of the f# static link options use the F# --full-help comandline switch of more details of these.

不。如果您想生成一个.exe,您可以使用一些f#静态链接选项,使用f# -全面帮助的comandline开关来提供更多的细节。

#4


6  

You can't compile F# source files (.fs) in a C# project, but you can add F# script files (.fsx) which you can use to reference and explore the C# project's assembly from F# interactive:

您不能在c#项目中编译f#源文件(.fs),但是您可以添加f#脚本文件(.fsx),您可以使用这些文件从f# interactive中引用和研究c#项目的程序集:

public static class Math
{
    public static double PowN(double d, int n)
    {
        var result = 1;
        for (int i = 0; i < n; i++) result *= d;
        return result;
    }
}

F# script file (.fsx):

f#脚本文件(.fsx):

#r "bin\debug\ClassLibrary1.dll"

Math.PowN(2.0,3)

#5


0  

Compile your F# code, "decompile" with any tool (like reflector, ilspy) to C# code, include it in a project -> Profit!

编译您的f#代码,“反编译”与任何工具(如reflector, ilspy)到c#代码,包括在一个项目中—>利润!

#1


21  

You can't include two different languages in the same project, but you can merge them using ilmerge. To do this, put both projects in the same solution and reference the F# module as you would any dll. As part of your deployment script, run ilmerge to combine the exe file and dll file into a single exe file. See this Code Project article that details how to use ilmerge to create an exe.

不能在同一个项目中包含两种不同的语言,但是可以使用ilmerge来合并它们。为此,将两个项目放在同一个解决方案中,并像引用任何dll一样引用f#模块。作为部署脚本的一部分,运行ilmerge,将exe文件和dll文件合并到一个exe文件中。请参阅这篇关于如何使用ilmerge创建exe的代码项目文章。

#2


15  

Not being able to mix C# and F# in the same project creates a problem; It leads to circular dependencies between the two projects. Conceptually there is only one project containing two languages, but because of the way visual studio manages projects languages cannot be mixed, leading to circular dependencies.

不能在同一个项目中混合c#和f#会产生问题;它导致两个项目之间的循环依赖。从概念上讲,只有一个项目包含两种语言,但是由于visual studio管理项目语言的方式不能混合,导致循环依赖。

For now the only solution I see it to create a lot of interfaces in a third project and have one of the project refer to the interfaces project, instead of the real project. Is there a better way?

目前,我看到的唯一解决方案是在第三个项目中创建大量接口,并让其中一个项目引用接口项目,而不是真正的项目。有更好的方法吗?

Kind Regards, Nick

亲切的问候,尼克

#3


12  

No. If you want to produce a single .exe you could use some of the f# static link options use the F# --full-help comandline switch of more details of these.

不。如果您想生成一个.exe,您可以使用一些f#静态链接选项,使用f# -全面帮助的comandline开关来提供更多的细节。

#4


6  

You can't compile F# source files (.fs) in a C# project, but you can add F# script files (.fsx) which you can use to reference and explore the C# project's assembly from F# interactive:

您不能在c#项目中编译f#源文件(.fs),但是您可以添加f#脚本文件(.fsx),您可以使用这些文件从f# interactive中引用和研究c#项目的程序集:

public static class Math
{
    public static double PowN(double d, int n)
    {
        var result = 1;
        for (int i = 0; i < n; i++) result *= d;
        return result;
    }
}

F# script file (.fsx):

f#脚本文件(.fsx):

#r "bin\debug\ClassLibrary1.dll"

Math.PowN(2.0,3)

#5


0  

Compile your F# code, "decompile" with any tool (like reflector, ilspy) to C# code, include it in a project -> Profit!

编译您的f#代码,“反编译”与任何工具(如reflector, ilspy)到c#代码,包括在一个项目中—>利润!