Posts

Showing posts with the label scripting

C# Scripting with Microsoft Roslyn

If you’re not familiar with Microsoft “Roslyn” please read Interactive C# Microsoft Roslyn . [TestFixture] public class TestScript { [Test] public void TestSimple() { var engine = new ScriptEngine(); Console.WriteLine(engine.Execute(" 1+1 ")); Console.WriteLine(engine.Execute< int >(" 1+1 ")); } [Test] public void TestSession() { var engine = new ScriptEngine(); var session = Session.Create(); engine.Execute(" int x = 1, y; ", session); engine.Execute(" if(x==1)y = 10; else y = 20; ",session); Console.WriteLine(engine.Execute(" y ", session)); } [Test] public void TestScriptFile() { var engine = new ScriptEngine(); ScriptContext context = new ScriptContext(); engine.ExecuteFile(" Test.csx ", context); } [Test] public void TestScriptFileHostObject() { var context = new ScriptContext(); var engine = new ScriptEn...

Interactive C# Microsoft Roslyn

Image
Microsoft Roslyn CTP adds Interactive C# window to Visual Studio 2010/2012 and adds support for scripting and interactive use of C# It also allows you to write script files *.csx The easiest way to initialize C# Interactive window is to right click on any C# project and choose 'Reset Interactive from Project' from context menu Then you can execute any C# code from your project public class Sample { public List< string > Run() { return new List< string >(){" One "," Two "," Three "," Four "}; } } To execute script use #load command Other useful commands #r " .\myproject.exe " //loads executable or dll #load " .\setup.csx " //loads script And this is good introduction to Microsoft “Roslyn”