Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 614489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:07:02+00:00 2026-05-13T18:07:02+00:00

Background Even though it’s possible to compile C# code at runtime, it’s impossible to

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:07:03+00:00Added an answer on May 13, 2026 at 6:07 pm

    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<string, object>, 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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i run my code, the picture box has a background colour, even though
I'm getting a issue with the image background repeating(see image). Even though i have
I'm trying to use background image in CSS but even though I gave the
The following code puts the two text elements at the top even though the
When I call a buttonHandler, the app just continues in the background even though
(Background info: ifelse evaluates both of the expressions, even though only one will be
Im trying to apply different background color to even and odd items in a
I use the following jquery statements, $(.resultsdiv:odd).css(background-color, #fff); $(.resultsdiv:even).css(background-color, #EFF1F1); $('.resultsdiv').hover(function() { $(this).css('background-color', '#f4f2f2');
Background: Firstly, I'm unsure if this is even called a metabox Here's an example
I need background transparent only for black div. but it is applying even for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.