问题
Is there a way to run F# code in C#? I have written an app in C# - I'd like to provide it the ability to execute F#, read Record objects, enumerate lists from F#. What is the current solution for this? I know in the future there will be probably be a way to do this via an update to Roslyn. Also, curious how to run F# code in F#, currently. Is there a way to do that easily?
回答1:
Currently, you have to make the F# a library, and then call it from C#. Since F# is just .NET, using the F# library is (mostly) just like using any other C# library. You might want to look into FSharpX for this portion, however, as it provides some utilities to ease calling F# from C# and vice versa.
If you want to compile the F# dynamically from C#, you'll need to use the F# CodeDom implementation from the F# Power Pack. This will let you use CodeDom to compile F# on the fly, and then execute it from C#.
Note that Roslyn will not help here, even when it's released, as it won't support F# as a code model, only C# and VB.Net. It would potentially be possible to use Roslyn to manipulate and compile C# from within F#, but not the other way around.
回答2:
In addition to the options that Reed already mentioned, there are a two other alternatives based on using the F# Interactive - you can either run it as a separate process or you could use the open-source release to reference it and call it directly (which is quite challenging, though).
So in summary, you have three options:
You can start the
fsi.exe
process and evaluate F# code in the F# Interactive environment (by sending F# code to the process by standard input). The F# binding for MonoDevelop is a good example how to do this (see InteractiveSession.fs).The only difficulty is communication between the two processes, but I think this should be doable using a .NET remoting (or WCF) channel between the two. You can also just read the standard output from F# Interactive, but that might be a bit too simple.
You could use the open-source release of F# compiler & tools to reference relevant parts of the F# compiler and call them (as a library) to evaluate F# code. This would be the best option and it would give you pretty much the same options you get with interactive C# from Roslyn.
Sadly, this is not quite an easy task - I was playing with the idea of doing this and I made some progress (I can share that), but it is not done yet. The idea is to take the source code of
fsi.exe
(here), remove all UI and turn it into a library.- Using CodeDOM, you can invoke the F# compiler (
fsc.exe
) to compile F# code into a stand-alone application (or library), load it dynamically and run it. A disadvantage is that this will start a new instance of the compiler each time you use it and so it may be a bit slow (depends on your scenario)
回答3:
Since the question has been asked, there has been a lot of exciting development :-)
You can now do this really easily using the F# Compiler Services project:
The F# compiler services package is a component derived from the F# compiler source code that exposes additional functionality for implementing F# language bindings, additional tools based on the compiler or refactoring tools. The package also includes F# interactive service that can be used for embedding F# scripting into your applications.
来源:https://stackoverflow.com/questions/15210254/run-f-code-in-c-sharp-like-c-sharp-in-c-sharp-using-roslyn