Alp*_*ral 5 f# codedom dynamic compileassemblyfromsource powerpack
我正在尝试使用基本程序来动态编译和运行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"
Run Code Online (Sandbox Code Playgroud)
来自这个网站:http://infsharpmajor.wordpress.com/2012/04/01/how-to-dynamically-synthesize-executable-f-code-from-text/
我遇到的问题是以下行:
let result = provider.CompileAssemblyFromSource( options, [|codeString|] )
Run Code Online (Sandbox Code Playgroud)
我得到例外的地方: The System cannot find the file specified.
我已经包含了所需的参考资料Fsharp.compiler.codeDom.dll,Fsharp.compiler.dll而且我不确定还有什么问题.我目前正在尝试CodeDom从codeplex 获取dll源代码并逐步完成它,但如果有人能够看到我忽略的一些问题,它将为我节省很多麻烦.
谢谢你的时间, - Alper
F# PowerPack 的 CodeDOM 实现底层使用 F# 编译器生成代码,这意味着它需要一种方法来查找编译器和相关元数据。
首先,您可能需要将FSharp.Core.sigdata文件复制到 bin 文件夹(元数据)。您可能还需要将 F# 编译器 (fsc.exe) 添加到您的路径中,或者只需将其复制到 bin 文件夹中(fsc.exe 位于 C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\ v4.0)。至少这对我在安装了 Visual Studio 2012 的 Windows 8 计算机上有效。