How to run F # script in a C # application context

Is it possible to easily run F # script from within (and in context) a C # application (host).

With "in context," I mean the following:

  • there is no separate process to execute the script
  • access from F # script to the static content of the host application (classes, properties, methods)

Basically, I'm looking for an API similar to this hypothetical API call

FSharpScriptRunner.RunInContext(string script); 

Please, help.

-Matthias

+7
source share
2 answers

There is currently no such api, but you can pretty easily do something similar in a couple of steps: -

1) Use the F # CodeDom (FSharp.Compiler.CodeDom.dll) available in PowerPack to dynamically compile your script. The power supply contains apis to compile a single file and allows you to add links to your current application, thereby providing script access to the classes, properties and methods of the host application.

2) At the end of the compilation phase, you should have a dynamic assembly in memory. Then you can use reflection to create a dynamic instance of the object from your script and execute it.

+9
source

Why don't you just compile the F # code into an assembly, and link to it from your C # application?

+2
source

All Articles