2
我想在C#中实现某种面向方面的编程,在那里我有一些小的成功,但发现一些重要的限制。如何拦截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),但没有静态方法。
任何有关如何捕获静态方法调用的建议或想法?
看起来像是它拦截函数调用的方式的限制。如果你告诉我们你是怎么做的,这可能会有所帮助。 – 2010-02-10 17:11:39
除了引入外部AOP框架之外,您是否曾经想出一个完成此操作的好方法? – 2011-10-13 14:52:13