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
This isn't possible. In the compiled .NET code (intermediate language), variables are represented simply as indices on the stack. For example the ldloc instruction, which loads a value of a variable takes only an unsigned int16
value as the parameter. There may be some way to do this for applications compiled in debug mode (after all, when debugging application, Visual Studio does that), but it cannot work in general.
In Phalanger (PHP compiler for .NET, which I'm partly involved in), this had to be somehow solved, because PHP language has eval
(it doesn't need to provide dynamic scoping, but it needs to access variables by name). So, Phalanger detects if a function contains any use of eval
and if it does, it stores all variables in Dictionary
, which is then passed to the eval
function (so that it can read variables by their name). I'm afraid this is the only way to do this...