views:

297

answers:

1

Hi All,

I'm trying to implement some sort of Aspect Oriented Programming in C#, where I had some small success but found some important limitations.

One of those limitations is the capability to intercept the call to a static method. For example, suppose We have the next object:

public class SampleObject  
{
    public SampleObjectProperty { get; set; }  

    public SampleObject(int anInteger) { this.SampleObjectProperty = anInteger; }

    public int SampleObjectMethod(int aValue) 
    { 
        return aValue + this.SampleObjectProperty; 
    }

    public static string GetPI() { return Math.PI.ToString(); }
}

The caller looks like:

[Intercept]
public class Caller : ContextBoundObject
{
    static void Main(string[] args)
    {
        SampleObject so = new SampleObject(1); // Intercepted successfully.
        so.SampleObjectProperty;               // Idem.
        so.SampleObjectProperty = 2;           // Idem.
        so.SampleObjectMethod(2);              // Idem.

        // The next call (invocation) executes perfectly, but is not intercepted.
        SampleObject.GetPI(); // NOT INTERCEPTED :(        
    }
}

With the code I have, I'm able to intercept the constructor, the instance method and the property (get and set), but no the static method.

Any suggestion or idea of how to capture the static method call?

Thanks a lot in advance.

+2  A: 

The AOP tools I have seen use other techniques to allow interception of static methods. In particular, I'm thinking of PostSharp, which alters your code post-compilation to insert the required interception instructions.

See http://www.postsharp.org/ for more information.

Interception using the ContextBoundObject technique is restricted to instance methods only.

Dave Cluderay