Search code examples
actionscript-3flashactionscriptintrospection

How to retrieve a function or method's argument / parameter list from outside the function in Actionscript 3


In another class elsewhere in the code, I want to access the parameters (and their types) of Foo.bar. The result would contain [ "a", Number ] and [ "b", String ] in one form or another.

public class Foo
{
    ...
    public function bar(a:Number, b:String):void
    {
        ...
    }
}

Solution

  • AS3 has a method called describeType

    If you call describeType(Foo) on the above example, you'll get:

    <type name="Foo" base="Class" isDynamic="true" isFinal="true" isStatic="true">
      <extendsClass type="Class"/>
      <extendsClass type="Object"/>
      <accessor name="prototype" access="readonly" type="*" declaredBy="Class"/>
      <factory type="Foo">
        <extendsClass type="Object"/>
        <method name="bar" declaredBy="Foo" 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="51"/>
          </metadata>
        </method>
        <metadata name="__go_to_definition_help">
          <arg key="pos" value="23"/>
        </metadata>
      </factory>
    </type>
    

    Now, you can use AS3's XML class and e4x to find the definition of the method with the name bar and grab the parameter elements.