2015-02-05 7 views
1

Я хотел бы найти рабочий пример использования C# для компиляцииAssemblyFromSource для кода F #. В моих текущих попытках я не могу создать компилятор, получив исключение из «NotSupportedException», сообщение «Компиляция не поддерживается». Я предполагаю, что я просто делаю это неправильно, так как F # Interactive работает и должен делать что-то подобное, но правильно.Выполнение F # Источник из C#

// 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); 

ответ

4

только этот код будет компилироваться, что вы хотите, есть дополнительный диагностический/отлаживать код остается в

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