Microsoft has released the Roslyn CTP which allows developers to use the Visual Basic and C# compilers as APIs. The snippet below can be used to execute C# code at runtime.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Roslyn.Scripting.CSharp;
using Roslyn.Compilers.CSharp;
using Roslyn.Compilers;
using System.Diagnostics;
using System.IO;
namespace ExpressionExecution
{
public partial class ExpressionEditor : Form
{
public ExpressionEditor()
{
InitializeComponent();
}
private void buttonRun_Click(object sender, EventArgs e)
{
ScriptEngine scriptEngine = new ScriptEngine();
try
{
object result = scriptEngine.Execute(textBoxSource.Text);
textBoxOutput.Text = result + "";
}
catch (Exception ex)
{
textBoxOutput.Text = ex.Message;
}
}
}
}
Output:
