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

让人想犯罪 __ 提交于 2019-12-29 04:09:27

问题


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?


回答1:


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.




回答2:


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.

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:


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.




回答4:


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:

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):

#r "bin\debug\ClassLibrary1.dll"

Math.PowN(2.0,3)



回答5:


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



来源:https://stackoverflow.com/questions/466298/is-it-possible-to-mix-cs-c-and-fs-f-files-in-a-single-visual-studio-wind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!