我正在尝试在 C# 中实现某种面向方面的编程,在那里我取得了一些小的成功,但发现了一些重要的限制。
这些限制之一是拦截对静态方法的调用的能力。例如,假设我们有下一个对象:
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(); }
}
调用者看起来像:
[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 :(
}
}
使用我拥有的代码,我能够拦截构造函数、实例方法和属性(get 和 set),但不能拦截静态方法。
关于如何捕获静态方法调用的任何建议或想法?