Modifying my own code while it's running

Started by
3 comments, last by Telastyn 10 years, 3 months ago

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?

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.

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.

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.

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.

This topic is closed to new replies.

Advertisement