【发布时间】:2013-08-10 20:10:49
【问题描述】:
我正在尝试编写一个基本程序来动态编译和运行 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,但我不确定还有什么问题。我目前正在尝试从 codeplex 中获取 CodeDom 的 dll 源代码并逐步完成它,但如果有人能够看到我忽略的一些问题,它会为我省去很多麻烦。
感谢您的宝贵时间, -阿尔珀
【问题讨论】:
-
以下链接包含内存编译的最少代码:stackoverflow.com/questions/15313404/…
标签: dynamic f# codedom powerpack compileassemblyfromsource