簡體   English   中英

從C#執行F#源

[英]Executing F# Source from C#

我想找到一個使用C#來編寫F#代碼的CompileAssemblyFromSource的工作示例。 在目前的嘗試中,我無法創建編譯器,並收到“ NotSupportedException”異常和“不支持編譯”消息。 我的猜測是我做錯了,因為F#Interactive可以工作並且必須做類似但正確的事情。

// C#
var source = "let add x y = x + y";
var cleanProvider = new FSharp.Compiler.CodeDom.FSharpCleanCodeProvider();
var compilerParams = new System.CodeDom.Compiler.CompilerParameters();
const string outfile = "C:\\temp\\FSFoo.EXE";
compilerParams.OutputAssembly = outfile;
compilerParams.GenerateExecutable = true;

var compiler = cleanProvider.CreateCompiler();
var compilerResults = compiler.CompileAssemblyFromSource(compilerParams, source);
var results = cleanProvider.CompileAssemblyFromSource(compilerParams, source);

僅此代碼即可編譯所需的代碼,其中還剩下其他診斷/調試代碼。

using FSharp.Compiler.CodeDom;
using System.CodeDom;
var codeString = "let add x y = x + y";
var provider = new FSharp.Compiler.CodeDom.FSharpCodeProvider();
Environment.CurrentDirectory.Dump("exe is going here"); // diagnostic helper
var targetFile = "FSFoo.exe";
provider .CompileAssemblyFromSource( new System.CodeDom.Compiler.CompilerParameters(){ OutputAssembly= targetFile, GenerateExecutable=true }, new []{codeString}).Dump(); // .Dump is just for diagnostics, remove if you aren't running this in linqpad
if(!System.IO.File.Exists(targetFile)){
    throw new FileNotFoundException("Could not find compiled exe",targetFile);
}

System.IO.Directory.GetFiles(Environment.CurrentDirectory,"FSFoo.exe").Dump();// .Dump is just for diagnostics, remove if you aren't running this in linqpad

其他可能有幫助的資源:

http://www.west-wind.com/presentations/dynamiccode/dynamiccode.htm

f#powerPack codeDom中的“ CompileAssemblyFromSource”

http://tiku.io/questions/638972/run-f-code-on-server-even-when-f-is-not-installed-there

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM