• Advertisement
Sign in to follow this  

Modifying my own code while it's running

This topic is 1475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, has anyone here successfully managed to rewrite their C#/.NET code while it's running?

 

What I want to do in particular is to wrap a method in a try..finally block with SomeDebugClass.MethodEntry() and SomeDebugClass.MethodExit() calls. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement

Visual Studio has an "edit and continue" option that you might be able to use.

 

When you have the proper compiler options set, you pause the code in the debugger, modify the code within a function, and resume execution. Visual Studio will hot-patch the program and run your new, modified code.

Share this post


Link to post
Share on other sites
You're better off using an aspect oriented programming framework like PostSharp to inject these things during a build step rather than at runtime. I'm not sure how viable ubiquitous runtime modification of a loaded assembly is.

Share this post


Link to post
Share on other sites

I should probably have been slightly more specific with my requirements. :)

 

I want to do this with code in the same application that is to be modified. I want to do highly localized instrumented profiling in a deployed or semi-deployed application/game. For example, a beta-tester is running my game which normally runs fine, but all of a sudden, he gets a serious framerate drop. Now I want a way to figure out what is causing it. Using dynamically inserted BeginSection and EndSection calls would be extremely nice and useful in this case.

Share this post


Link to post
Share on other sites
Enh, up I imagine the runtime modification would screw up your profiling more than simply including it always (in beta) and turning on logging when needed. When done judiciously, it won't substantially impact performance but still give you plenty of instrumentation to identify issues.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement