简体   繁体   English

从C#执行F#源

[英]Executing F# Source from C#

I'd like to find a working example of using C# to CompileAssemblyFromSource of F# code. 我想找到一个使用C#来编写F#代码的CompileAssemblyFromSource的工作示例。 In my current attempts, I'm unable to create the compiler, getting an exception of "NotSupportedException", message of "Compilation not supported." 在目前的尝试中,我无法创建编译器,并收到“ NotSupportedException”异常和“不支持编译”消息。 My guess is I'm just doing it wrong, since F# Interactive works and has to be doing something similar, but correctly. 我的猜测是我做错了,因为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);

this code alone will compile what you want, there is extra diagnostic/debug code left in. 仅此代码即可编译所需的代码,其中还剩下其他诊断/调试代码。

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

other resources that may help: 其他可能有帮助的资源:

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

"CompileAssemblyFromSource" in f# powerPack codeDom f#powerPack codeDom中的“ CompileAssemblyFromSource”

http://tiku.io/questions/638972/run-f-code-on-server-even-when-f-is-not-installed-there 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