Dynamic &引用;CompileAsemblyFromSource“;在f#powerPack codeDom中
我正在尝试运行一个基本程序来动态编译和运行f#代码。我正在尝试运行以下代码:Dynamic &引用;CompileAsemblyFromSource“;在f#powerPack codeDom中,dynamic,f#,codedom,powerpack,compileassemblyfromsource,Dynamic,F#,Codedom,Powerpack,Compileassemblyfromsource,我正在尝试运行一个基本程序来动态编译和运行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
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"
我得到异常:系统找不到指定的文件。
我已经包括了所需的参考Fsharp.compiler.codeDom.dll
和Fsharp.compiler.dll
,我不确定还有什么问题。我目前正试图从codeplex中获取CodeDom
的dll源代码,并逐步完成它,但如果有人能够看到我忽略的一些问题,这会让我省去很多麻烦
谢谢你抽出时间,
-Alper在F#PowerPack的CodeDOM实现下面,使用F#编译器生成代码,这意味着它需要找到编译器和相关元数据的方法
首先,您可能需要将文件复制到bin文件夹(元数据)。您可能还需要将F#编译器(fsc.exe)添加到路径中,或者将其复制到bin文件夹中(fsc.exe位于C:\Program Files(x86)\Microsoft SDK\F#\3.0\Framework\v4.0)。在安装了Visual Studio 2012的Windows 8计算机上,这至少对我有效。以下链接包含内存编译的最少代码:此问题似乎相关
let result = provider.CompileAssemblyFromSource( options, [|codeString|] )