Background
Even though it's possible to compile C# code at runtime, it's impossible to include and run the generated code in the current scope. Instead all variables have to be passed as explicit parameters.
Compared with dynamic programming languages like Python, one could never truly replicate the complete behaviour of eval
(as in this example).
x = 42
print(eval("x + 1")) # Prints 43
The question
So my question is (regardless if it's actually useful ;)) whether it's possible to mimic dynamic scope in .NET through the use of reflection.
Since .NET provides us with the Diagnostics.StackTrace
class which allows us to inspect the calling methods, this question boils down to the following: (How) is it possible to reliably access the locals of calling methods?
Does the stack trace provide us with enough information to compute the memory offsets or are such things forbidden in managed code anyway?
Is such code somehow possible?
void Foo() {
int x = 42;
Console.WriteLine(Bar());
}
int Bar() {
return (int)(DynamicScope.Resolve("x")); // Will access Foo's x = 42
}