如何截获一类方法调用 [英] How to intercept method call of a class

查看:85
本文介绍了如何截获一类方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正是有一类的拦截方法调用,所以你可以做的AOP?

Is there anyway to intercept method call of a class so you can do AOP?

例如。

我想Teacher.Talk()在两种情况下执行不同的:

I want the Teacher.Talk() performs differently in two scenarios:

class School
{
    [Fun]
    public void Picnic {
        Teacher t = new Teacher();
        t.Talk();
    }

    public void Seminar{
        Teacher t = new Teacher();
        t.Talk();
    }
}

在上面的code,功能野餐是由乐趣属性的装饰,所以老师的通话功能多比在研讨会函数不是由该属性的装饰更有趣。

In above code, the function Picnic is decorated by Fun attribute, so the Talk function of the teacher is much more interesting than the one in Seminar function which is not decorated by the attribute.

我检查Castle.DynamicProxy,但它使用代理类,需要一些code修改。这无助于解决我的问题,因为我想使用的属性进行配置,这样,当决策的变化,很少有code的修改是必要的。

I've checked Castle.DynamicProxy, but it uses proxy class and need some code modification. That doesn't help to solve my problem because I want to use the attribute to do the configuration, so that when the decision changes, very few code modifications will be needed.

感谢很多提前!

推荐答案

有两种基本方法:创建一个子类,代理或重整的code又名编译时织进入钩入编译的程序集。

There are two basic approaches: creating a subclass proxy or mangling the code aka "compile-time weaving" to enter the hooks into the compiled assembly.

子类只允许你拦截虚方法和构造函数,而编译时编织可以在任何地方进入钩子拦截在codeBase的电话。

Subclassing only lets you intercept virtual methods and constructors while compile-time weaving can enter hooks anywhere to intercept calls in the codebase.

这篇关于如何截获一类方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆