F# PowerPack CodeDom中的"CompileAssemblyFromSource"

5

我正在尝试编写一个基本程序,以动态编译和运行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"

我会尽力帮助您翻译中文。以下是需要翻译的内容:

来自此网站:http://infsharpmajor.wordpress.com/2012/04/01/how-to-dynamically-synthesize-executable-f-code-from-text/

我遇到的问题是以下代码行:

let result = provider.CompileAssemblyFromSource( options, [|codeString|] ) 

我在哪里遇到了异常:系统找不到指定的文件。

我已经包含了所需的引用Fsharp.compiler.codeDom.dllFsharp.compiler.dll,但我不确定还有什么其他问题。我正在尝试从Codeplex获取CodeDom的dll源代码并进行步骤分析,但如果有人能看到我忽略的问题,那将节省我很多麻烦。

谢谢您的时间, -Alper


以下链接包含了内存编译的最小代码示例:https://dev59.com/X2_Xa4cB1Zd3GeqP02-6 - Phillip Trelford
这个问题似乎与https://dev59.com/RnE85IYBdhLWcg3whT9r有关。 - Phillip Trelford
1个回答

3

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机器上,这个方法是可行的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接