I donβt know if there is a specific Roslyn API, but it looks like you can get the information using Reflection. This is a bit troublesome. I am using REPL in Visual Studio to get closer to your situation.
using System.Reflection; Assembly.GetExecutingAssembly().DefinedTypes
Indicates that there is a type for each iteration of the loop, plus some additional ones for certain classes. Fortunately for you, the types of Submission are numbered with the latest messages having a larger number ie Submission#11 appears after Submission#5 .
Variables defined in the REPL are displayed as a field in the session type that corresponds to the execution of the loop in which it was defined. The call to view all the defined variables in the type will be as follows:
chosenType.GetFields(BindingFlags.Instance | BindingFlags.Public)
Roslyn REPL allows you to re-declare variables, masking previously announced from new subscribers. Now consider the βsessionβ as a stack of all materials (the oldest of them below). If you go down the stack, the first entry of the given field name will be the active variable with that name in REPL. The received field information provides a type in addition to the name.
Here is an example session (including output) showing how you can see all the variables declared in the session.
> using System.Reflection; > var a = 1; > var b = "c"; > var c = from type in Assembly.GetExecutingAssembly().DefinedTypes.Reverse() from variable in type.GetFields(BindingFlags.Instance | BindingFlags.Public) select variable; > foreach (var info in c ) { if (info.FieldType != typeof(Roslyn.Services.InteractiveHostObject)) { Console.WriteLine(info); } } System.Collections.Generic.IEnumerable`1[System.Reflection.FieldInfo] c System.String b Int32 a
If the same name appears twice, this is the first one available in the session. The above advantage is that type returns usually age the youngest. You probably want to sort it yourself to be sure.
For some reason (maybe an error?) I should use if statements in the foreach loop using the LINQ where clause, which does not produce the expected results.
There are many reflection methods that can be useful depending on what you want to find, such as GetMethods and GetEvents . If you want everything, that is GetMembers . See Type Methods .
I hope someone can provide an easier way.