问题
Long time reader, very first question. fsi.exe is a .NET executable and therefore contains its own assembly complete with all the yummy methods and whatnot fsi uses to execute F# scripts.
Looking at the assembly in .NET Reflector (pick your class, but Shell is the best example) reveals a bunch of garbage* names that look like decorated C++ functions (say, from Dependency Walker). Incidentally and slightly beside the point, F# assemblies compile in much the same way, with lots of garbage* names, which leads me to think fsi.exe was written in F#, perhaps as a proof-of-usability?
Anyway, here's my question: has anyone delved into fsi.exe and figured out how to embed it into a .NET application? Because I'd like to use F# as a scripting language, but programs compile to (surprise) programs, and scripts must be executed by fsi.exe, which is unacceptable in my domain (I need a persistent VM). I don't expect a how-to guide on using fsi.exe, but I'm curious to know if anyone has played with it and, if so, what have you discovered about how it works?
Thanks for your time.
* Garbage at a casual glance. Obviously they are formatted this particular way for a particular reason that is beneath the hood.
回答1:
As far as I know, fsi.exe
doesn't expose any API that you could use to embed it in your application (e.g. to implement scripting). I think there are essentially two options:
If you want to use existing
fsi.exe
directly, the only way is to start it using .NETProcess
class and communicate with it somehow. This should be doable - you can send commands to the process using standard input. To read output back, you can use standard output, but this gives you only limited information. It probably would be possible to use .NET Remoting to communicate betweenfsi.exe
and your application (e.g. your objects loaded in script context in FSI would send information to your application via Remoting).Another alternative is to use the open-source release of F# and modify
fsi.exe
to expose all information you need as an API. This requires more effort in order to understand howfsi.exe
works, but it should be doable. You probably don't need that many additions - the state maintained by FSI contains things like global variables.
Regarding the license - you probably cannot use fsi.exe
distributed with Visual Studio. I'm not sure about fsi.exe
that comes with the CTP release. Compiling it from source (available here) will be definitely fine (because it has open-source release).
回答2:
There's some working code here:
http://www.codeproject.com/Articles/241577/Embedded-scripting-using-Fsharp
来源:https://stackoverflow.com/questions/4637181/fsi-exe-assembly-anyone-know-how-to-embed-it