To compile F# code dynamically, you will need to use the FSharpCodeProvider class. Using it is hardly different from using the CodeDomProvider class
First, you need to add the following reference:
FSharp.Compiler.CodeDom.dll
Then add the following using statements:
using System.CodeDom.Compiler
using Microsoft.FSharp.Compiler.CodeDom;
The code is below.
public static void CompileFSharp(string sourceCode)
{
CompilerResults compilerResults;
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.GenerateExecutable = true;
compilerParameters.OutputAssembly = "FSharpExecutable.exe";
var fsharpCodeProvider = new FSharpCodeProvider();
compilerResults = fsharpCodeProvider.CompileAssemblyFromSource(compilerParameters, sourceCode);
if(compilerResults.Errors.HasErrors)
{
// Handle compiler errors
}
}
If I have made any mistakes in the post, please do not hesitate to inform me about them.
No comments:
Post a Comment