0

我经常遇到参数不匹配错误,这通常需要我很多时间来调试程序。该死的,我真的希望我知道函数的入口要求以及它们来自哪里。

因为我只知道函数变量是函数,所以没有任何其他信息。我写了大量这样的代码

    public static function call(func:Function,params:Array = null,addToTailIfNotNull:*=null):void{
        if (func!=null){
            var args:Array =[];
            if(params!=null){
                args = ArrayTools.clone(params); 
            }
            if (addToTailIfNotNull!=null){
                args.push(addToTailIfNotNull);
            }
            func.apply(null,args);
        }
    }

我应该更聪明地做事。

4

1 回答 1

2

I can suggest you using flash.utils.describeType() method. It returns an XML with a description of an object you passed as a parameter.

Lets say you have a Class:

public class Example {
    public function someMethod(number:Number, string:String):void {

    }
}

And you call somewhere:

flash.utils.describeType(Example);

You should get an XML with something like this in there:

<method name="someMethod" declaredBy="com.example::Example" returnType="void">
  <parameter index="1" type="Number" optional="false"/>
  <parameter index="2" type="String" optional="false"/>
  <metadata name="__go_to_definition_help">
    <arg key="pos" value="501"/>
  </metadata>
</method>

I am not sure that this is what you looking for, as in your example if you pass you Function argument there you will get a description of Function class:

<type name="builtin.as$0::MethodClosure" base="Function" isDynamic="false" isFinal="true" isStatic="false">
  <extendsClass type="Function"/>
  <extendsClass type="Object"/>
  <accessor name="length" access="readonly" type="int" declaredBy="Function"/>
  <accessor name="prototype" access="readwrite" type="*" declaredBy="builtin.as$0::MethodClosure"/>
</type>

But maybe you can refactor you "call" method so it could get the right description (for example pass additional info into it - like an object class and a method name - so you could analyse the method signature in it. Not the most beautiful solution, but still...)

于 2012-09-27T15:07:56.533 回答