Skip to content

Instantly share code, notes, and snippets.

@Pilchie
Created March 5, 2013 02:33
Show Gist options
  • Save Pilchie/5087540 to your computer and use it in GitHub Desktop.
Save Pilchie/5087540 to your computer and use it in GitHub Desktop.
Getting variables declared in a session.
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