Last-Minute Whidbey Feature Request: Easy Global Interception#

While we wait for decent AOP frameworks in .NET that can handle this kind of stuff, wouldn't it be great if we could just do some basic interception like the code below? We can't have every single object inherit from MarshalByRefObject to get some interception goodies now can we? At least we'd get rid of boilerplate code added to every single method (who wants to type hard to maintain logging code manually anyway, or have it generated and still clutter your code view).

Of course this has some major security implications (if you can hook into any method and look at it's parameters you can easily sniff passwords or alter parameters or look at private methods you weren't intended to see). But I guess these could be fixed by allowing only your own methods (i.e. in the same assembly) to be intercepted.

As for the performance hit, well, in the easiest cases where the MethodInfo parameter isn't used (to 'switch' on certain methods for example) the compiler or JIT could inline this stuff altogether (change obj to this and replace the args with their respective parameter names). Otherwise, there'll be a pretty serious performance penalty but then again, you get some serious benefits and you can enable it dynamically only in case there are problems on your production machines. When disabled, the code won't even be run so it'll perform at a whopping 100%.

CLR guys? Got some spare time before the first Whidbey beta? Robert? Got some spare time to push this up the agenda? ;-)

public void Initialize()
{
    // Only allow this if we have problems or while debugging.
    if( enableInterception )
    {
        // Hook up some event handlers to get interception on method entry and exit.
        System.Reflection.MethodInfo.MethodEntered += new MethodEnteredEventHandler( MethodEntered );
        System.Reflection.MethodInfo.MethodLeft += new MethodLeftEventHandler( MethodLeft );
    }
}

/// <summary>
/// Called when any method is entered.
/// </summary>
/// <param name="info">The runtime information about the method being entered.</param>
/// <param name="obj">The object on which the method is being called, or null if it is a static method.</param>
/// <param name="args">The arguments passed to the method.</param>
/// <remarks>
/// This method is not called for event handlers of the <see cref="System.Reflection.MethodInfo.MethodEntered"/> event
/// to avoid an infinite loop.
/// </remarks>
public void MethodEntered( System.Reflection.MethodInfo info, object obj, object[] args )
{
    // Insert your logging, security, pre-condition check, ... code here.
    System.Diagnostics.Debug.WriteLine( "Method entered: " + info.Name );
}

/// <summary>
/// Called when any method is left.
/// </summary>
/// <param name="info">The runtime information about the method being left.</param>
/// <param name="obj">The object on which the method is being called, or null if it is a static method.</param>
/// <param name="outArgs">The byref and out arguments of the method.</param>
/// <param name="returnValue">The return value of the method, or null if it is a void method.</param>
/// <param name="exception">The exception that caused the method to be left, or null if the method exited normally.</param>
/// <remarks>
/// This method is not called for event handlers of the <see cref="System.Reflection.MethodInfo.MethodLeft"/> event
/// to avoid an infinite loop.
/// </remarks>
public void MethodLeft( System.Reflection.MethodInfo info, object obj, object[] outArgs, object returnValue, object exception )
{
    // Insert your logging, security, post-condition check, ... code here.
    System.Diagnostics.Debug.WriteLine( "Method left: " + info.Name );
}

Comments are closed.
All content © 2012, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 350
This Year: 0
This Month: 0
This Week: 0
Comments: 530
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In