Die gewünschte kann mit F# CodeDom provider erreicht werden. Ein minimales ausführbares Snippet unten zeigt die erforderlichen Schritte. Es nimmt einen beliebigen, vermutlich korrekten F # -Code aus einem String und versucht, diesen in eine Assembly-Datei zu kompilieren. Wenn dies erfolgreich ist, lädt es diese gerade synthetisierte Assembly aus der Datei dll
und ruft eine bekannte Funktion von dort auf, andernfalls zeigt es, was das Problem beim Kompilieren des Codes ist.
open System
open System.CodeDom.Compiler
open Microsoft.FSharp.Compiler.CodeDom
// Our (very simple) code string consisting of just one function: unit -> string
let codeString =
"module Synthetic.Code\n let syntheticFunction() = \"I've been compiled on the fly!\""
// Assembly path to keep compiled code
let synthAssemblyPath = "synthetic.dll"
let CompileFSharpCode(codeString, synthAssemblyPath) =
use provider = new FSharpCodeProvider()
let options = CompilerParameters([||], synthAssemblyPath)
let result = provider.CompileAssemblyFromSource(options, [|codeString|])
// If we missed anything, let compiler show us what's the problem
if result.Errors.Count <> 0 then
for i = 0 to result.Errors.Count - 1 do
printfn "%A" (result.Errors.Item(i).ErrorText)
result.Errors.Count = 0
if CompileFSharpCode(codeString, synthAssemblyPath) then
let synthAssembly = Reflection.Assembly.LoadFrom(synthAssemblyPath)
let synthMethod = synthAssembly.GetType("Synthetic.Code").GetMethod("syntheticFunction")
printfn "Success: %A" (synthMethod.Invoke(null, null))
else
failwith "Compilation failed"
gefeuert-up erzeugt es die erwartete Ausgabe
Success: "I've been compiled on the fly!"
Wenn Sie gehend mit dem Snippet spielen es Referenzierung FSharp.Compiler.dll
und FSharp.Compiler.CodeDom.dll
erfordert. Genießen!
Es könnte sich lohnen, dies zu betrachten [http://stackoverflow.com/questions/372043/how-can-evaluate-an-expression-stored-as-a-string-in-f] –
Sie könnte auch die [f # codedom] (http://stackoverflow.com/questions/2608114/f-equivalent-to-eval) –