我知道使用动态添加到动态代理类的函数很容易,但我也希望能够拦截对类型函数的调用,就像 Python 中的装饰器一样。下面的例子。我希望能够通过'typedFunc'的'callProperty'就像它已经通过'dynFunc'一样,
package
{
import flash.display.Sprite;
[SWF(width = '400', height = '400')]
public class Test extends Sprite
{
public function Test()
{
var t:TypeTest = new TypeTest();
t.dynFunc = function dynFunc(s:String, i:int):Boolean { return true; };
t.typedFunc("a", 1);
t.dynFunc("b", 2);
}
}
}
import flash.utils.Proxy;
import flash.utils.flash_proxy;
internal dynamic class TypeTest extends Proxy
{
private var customs:Object = new Object();
override flash_proxy function callProperty(name:*, ...parameters):* {
var retval:* = (this[name] as Function).apply(null, parameters);
trace("called", name, "with", parameters);
return retval;
}
public function typedFunc(s:String, i:int):Boolean {
return false;
}
override flash_proxy function getProperty(name:*):* { return customs[name]; }
override flash_proxy function setProperty(name:*, value:*):void { customs[name] = value; }
}