المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Is there a way in .NET to access the bytecode/IL/CLR that is currently running?



C# Programming
04-14-2010, 10:26 AM
Hi.

I'd like to have access to the bytecode that is currently running or about to run in order to detect certain instructions and take specific actions (depending the instructions). In short, I'd like to monitor the bytecode in order to add safety control (see DETAILS below for explanation).

Is this possible? I know there are some AOP frameworks that notify you of specific events, like an access to a field or the invocation of a method, but I'd like to skip that extra layer and just look at all the bytecode myself, throughout the entire execution of the application.

I've already looked at several AOP frameworks (although not in great detail, since they don't seem to do quite what I need) and I'm familiar with Mono.Cecil.

I appreciate alternative suggestions, but I don't want to introduce the overhead of an AOP framework when what I actually need is access to the bytecode, without all the stuff they add on top to make it more user-friendly (... admittedly very useful stuff when you don't want to go low-level).

Thanks http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif


DETAILS:

Basically, I have a C# application and I need to monitor the instructions it wants to run in order to detect read or write operations to fields (operations Ldfld and Stfld) and insert some instructions before the read/write takes place: I may need to acquire locks, or if that fails abort the operation. Also, I may need to update a read log (in case of a read) or write log (in case of a write).

In fact, what I'd really like to do is to replace the read/write instruction with my own custom code, but it that fails I think I could manage just inserting some instructions before and after.</br>