f# コードを動的にコンパイルして実行するための基本的なプログラムを作成しようとしています。次のコードを実行しようとしています。
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"
私が抱えている問題は、次の行にあります。
let result = provider.CompileAssemblyFromSource( options, [|codeString|] )
例外が発生する場所:The System cannot find the file specified.
必要な参照を含めましたが、Fsharp.compiler.codeDom.dll
他Fsharp.compiler.dll
に何が問題なのかわかりません。私は現在CodeDom
、codeplex から dll ソース コードを取得してステップ実行しようとしていますが、見落としている問題を誰かが見ることができれば、頭痛の種を大幅に減らすことができます。
お時間をいただきありがとうございます - アルパー