简体   繁体   English

是否可以在单个Visual Studio Windows控制台项目中混合使用.cs(C#)和.fs(F#)文件? (。净)

[英]Is it possible to mix .cs (C#) and .fs (F#) files in a single Visual Studio Windows Console Project? (.NET)

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#中?

You can't include two different languages in the same project, but you can merge them using ilmerge . 您不能在同一个项目中包含两种不同的语言,但可以使用ilmerge合并它们。 To do this, put both projects in the same solution and reference the F# module as you would any dll. 要做到这一点,将两个项目放在同一个解决方案中并像引用任何dll一样引用F#模块。 As part of your deployment script, run ilmerge to combine the exe file and dll file into a single exe file. 作为部署脚本的一部分,运行ilmerge将exe文件和dll文件合并到一个exe文件中。 See this Code Project article that details how to use ilmerge to create an exe. 请参阅此Code Project文章,其中详细介绍了如何使用ilmerge创建exe。

Not being able to mix C# and F# in the same project creates a problem; 无法在同一个项目中混合C#和F#会产生问题; 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. 从概念上讲,只有一个项目包含两种语言,但由于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 亲切的问候,尼克

不。如果您想生成单个.exe,您可以使用一些f#静态链接选项,使用F#--full-help comandline切换这些更多细节。

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)

编译你的F#代码,用任何工具(如反射器,ilspy)“反编译”到C#代码,将它包含在项目中 - >利润!

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

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