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 EDIT #1 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 the following questions (...among many many others ;) ):
Preprocessing C# - Detecting Methods
What CLR/.NET bytecode tools exist?
as well as 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 :)
EDIT #1: more details on what I want to do.
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.
EDIT #2: PostSharp
Dave suggests I use PostSharp
The problem is at compile time I still don't know which classes I'm going to need to weave, so I'd like to delay this until they are loaded. As I understand, this isn't possible with PostSharp? Please correct me if I'm wrong.
I know PostSharp does load-time static weaving, but apparently "there is currently no "off-the-shelf" way to perform it" (notice however that this post is from PostSharp 1.5; maybe this has changed). I guess it would still be easier than just doing everything myself, but I can't find any info on how PostSharp would help me in this case. I guess I'll ask in the PostSharp forums.