" CompileAssemblyFromSource"在f#powerPack codeDom [英] "CompileAssemblyFromSource" in f# powerPack codeDom
问题描述
打开系统
打开System.CodeDom.Compiler
打开Microsoft.FSharp.Compiler.CodeDom
//我们(非常简单)的代码字符串只包含一个函数:unit - > string
let codeString =
module Synthetic.Code\\\
let syntheticFunction()= \我已经在编译了!\
//装配路径保留编译代码
let synthAssemblyPath =synthetic.dll
let CompileFSharpCode(codeString,synthAssemblyPath)=
use provider = new FSharpCodeProvider()
let options = CompilerParameters ([||],synthAssemblyPath)
let result = provider.CompileAssemblyFromSource(options,[| codeString |])
//如果我们错过了任何东西,让编译器向我们说明问题是什么
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
如果CompileFSharpCode(codeString,synthAssemblyPath)然后
let synthAssembly = Reflection.Assembly.LoadFrom(synthAssemblyPath)
let synthMethod = synthAssembly.GetType(Synthetic.Code)。 GetMethod(syntheticFunction)
printfnSuccess:%A(synthMethod.Invoke(null,null))
else
failwith编译失败
我遇到的问题是下列行:
let result = provider.CompileAssemblyFromSource(options,[| codeString |])
我收到例外:系统找不到指定的文件。
我已经包含所需的引用 Fsharp.compiler.codeDom.dll
和 Fsharp.compiler.dll
和我不知道还有什么可能的问题。我目前正在尝试从Codeplex获取 CodeDom
的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)。至少这在我的Windows 8机器上安装了Visual Studio 2012。
I am trying to get going a basic program to dynamically compile and run f# code. I am trying to run the following code:
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"
from this site: http://infsharpmajor.wordpress.com/2012/04/01/how-to-dynamically-synthesize-executable-f-code-from-text/
The issue I am having is with the following line:
let result = provider.CompileAssemblyFromSource( options, [|codeString|] )
Where I get the exception: The System cannot find the file specified.
I have included the required references Fsharp.compiler.codeDom.dll
and Fsharp.compiler.dll
and I am not sure what else could be there issue. I am currently trying to get the dll source code for CodeDom
off of codeplex and step through it but it would save me a lot of headache if somebody is able to see some issue I am overlooking.
Thank you for your time, -Alper
Underneath the F# PowerPack's CodeDOM implementation uses the F# compiler to generate code, which means it needs a way to find the compiler and related metadata.
First off you may need to copy the FSharp.Core.sigdata file to your bin folder (the metadata). You may also need to add the F# compiler (fsc.exe) to your path or alternatively just copy it into your bin folder (fsc.exe is in C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0). Atleast this worked for me on my Windows 8 machine with Visual Studio 2012 installed.
这篇关于" CompileAssemblyFromSource"在f#powerPack codeDom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!