首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"CompileAssemblyFromSource“( f# powerPack codeDom )

"CompileAssemblyFromSource“( f# powerPack codeDom )
EN

Stack Overflow用户
提问于 2013-08-08 22:42:16
回答 1查看 755关注 0票数 5

我正在尝试使用一个基本程序来动态编译和运行f#代码。我试图运行以下代码:

代码语言:javascript
复制
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/

我面临的问题是以下几点:

代码语言:javascript
复制
let result = provider.CompileAssemblyFromSource( options, [|codeString|] ) 

在这里我得到了例外:The System cannot find the file specified.

我已经包含了所需的参考资料Fsharp.compiler.codeDom.dllFsharp.compiler.dll,我不知道还会有什么问题。我目前正在尝试从codeplex中获取CodeDom的dll源代码,并逐步完成它,但是如果有人能够看到我忽略的一些问题,它会让我省去很多麻烦。

谢谢你抽出时间,-Alper

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-09 07:18:37

在F# PowerPack的CodeDOM实现下,使用F#编译器生成代码,这意味着它需要找到编译器和相关元数据的方法。

首先,您可能需要将FSharp.Core.sigdata文件复制到bin文件夹(元数据)。您还可能需要将F#编译器(fsc.exe)添加到路径中,或者只需将其复制到bin文件夹中(fsc.exe位于C:\Program (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0中)。至少在我安装了Visual 2012的Windows 8计算机上,这是可行的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18137459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档