开发者

How to get the argument's type of a function?

I want to guess the type of every argument from an anonymous function, something like mapping a class with reflection but just for a function, something like...

public function guessMyArgumentType(f:Function):void {

    for each (argument:* in f.arguments) {

        trace(typeof(argument));

    }

}

thanks!

UPDATE

Maybe this is insane but what about:

public static function guessMyArgumentType(f:Function):String {

    try {

        f(2);
        return "int";

    } catch (error:Error) {

        try {

            f("a");
            return "String";

        } catch (error:Error) {

            开发者_如何学Pythontry {

                f(["uno", "dos", "tres"]);
                return "Array";

            } catch (error:Error) {

                try {

                    f(new ArrayCollection(["uno", "dos", "tres"]));
                    return "ArrayCollection";

                } catch (error:Error) {

                    return "WTF?";

                }

            }

        }

    }

}


flash.utils.getQualifiedClassName() as referenced here: Get the type in flex

The arguments array is only accessible from inside a given function.

I don't think what you're asking is doable unless you do some crazy try/catch thing but that would always fail if the argument were a custom class so it's not a great solution.

Another option would be adding some custom metadata which describes the function arguments. [YourMeta(arg1="String",arg2="Array")] above each function and then you get use ObjectUtil.getClassInfo(f).metadata to find that info. This requires you add YourMeta to the compiled metatags args.

There might be another way through something not published to ASDocs but I don't know about it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜