【问题标题】:Executing F# Source from C#从 C# 执行 F# 源代码
【发布时间】:2015-02-05 19:04:18
【问题描述】:

我想找到一个使用 C# 来 CompileAssemblyFromSource 的 F# 代码的工作示例。在我目前的尝试中,我无法创建编译器,收到“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);

【问题讨论】:

    标签: c# f# codedom


    【解决方案1】:

    仅此代码将编译您想要的,还有额外的诊断/调试代码。

    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

    "CompileAssemblyFromSource" in f# powerPack codeDom

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

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 2023-03-13
      • 2017-07-26
      • 2015-08-29
      相关资源
      最近更新 更多