如何在C#中拦截静态方法调用

发布于 2024-08-20 22:54:00 字数 1082 浏览 4 评论 0原文

我正在尝试在 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 :(        
    }
}

使用我拥有的代码,我能够拦截构造函数、实例方法和属性(获取和设置),但不能拦截静态方法。

关于如何捕获静态方法调用有什么建议或想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2024-08-27 22:54:01

我见过的 AOP 工具使用其他技术来拦截静态方法。
特别是,我正在考虑 PostSharp,它会在编译后更改代码以插入所需的拦截指令。

请参阅 http://www.postsharp.org/ 了解更多信息。

使用 ContextBoundObject 技术的拦截仅限于实例方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文