Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to delete or insert frames into the C# call stack?

Tags:

c#

callstack

Using StackTrace I can get the stack frames. My question is whether it is possible to manipulate the call stack in C#? More specifically: Is it possible...

  • to insert a frame into the call stack? or

  • to delete a frame from it?

like image 236
user927432 Avatar asked Oct 19 '25 03:10

user927432


2 Answers

Stack is a very internal component of program's runtime. Having the ability to alter it would make it possible to:

  • Make almost unrestricted goto's. Something that is considered a very bad programming practice and is not possible even in PHP. Debugging such thing would be a complete mess.
  • Inject code on method returns. This may be useful, but is already covered by aspect oriented tools, like PostSharp or Spring.NET, which allow you to do this in clean and predictable way. And they cover much more cases than just method returns.

Thus, I do not think there is any programming language that allows you to manipulate stack explicitly. It would make things very messy and any benefits it may produce are already covered by Aspect-Oriented-Programming. I bet AOP will make you able to achieve what you want.

like image 198
Marcin Avatar answered Oct 21 '25 17:10

Marcin


It is easy to manipulate the stack in Smalltalk, but I don't think it is possible in C#.

like image 31
Mateusz Grotek Avatar answered Oct 21 '25 15:10

Mateusz Grotek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!