Created
March 5, 2013 02:33
-
-
Save Pilchie/5087540 to your computer and use it in GitHub Desktop.
Getting variables declared in a session.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Roslyn.Scripting.CSharp; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ScriptEngine e = new ScriptEngine(); | |
e.AddReference(typeof(object).Assembly); | |
e.AddReference(typeof(Enumerable).Assembly); | |
e.AddReference(typeof(Assembly).Assembly); | |
e.ImportNamespace("System"); | |
e.ImportNamespace("System.Linq"); | |
e.ImportNamespace("System.Reflection"); | |
var s = e.CreateSession(); | |
s.Execute("var myField = 42;"); | |
var fields = (IEnumerable<FieldInfo>)s.Execute(@"Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Name.StartsWith(""Submission"")).SelectMany(t => t.GetFields())"); | |
foreach (var f in fields) | |
{ | |
Console.WriteLine(f.Name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment