How can I list all the const properties defined in a class

我与影子孤独终老i 提交于 2019-12-11 14:16:04

问题


How can i list all the names (and values) of public (and private / protected) const defined in a class ?

public class Layers {

    public const BACKGROUND:String = "background";
    public const PARENT:String = "parent";
    public const MAP:String = "map";
    public const LINES:String = "lines";
    public const POINTS:String = "points";
    public const WINDOWS:String = "windows";

    ... 

    public function isValidValue(type:String) {
        // ...           
        // if type is a value of a constant return TRUE
        // ...
    }

}

回答1:


At runtime, you can use describeType() to list all the public vars (not too sure about consts), and a whole lot more info too.

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#describeType()

Privates are more tricky to get.

Not sure if it wouldn't be quicker to create an array of the constants and then use array.indexOf(type)

P.S. I also believe there is a JSON version of describeType() now, somewhere.




回答2:


This works with as3 and flex 4.5.1

public static function isValidValue(type:String):Boolean {

        var m_constNameList:XMLList = describeType(Layers).descendants("constant");

        for each(var obj:Object in m_constNameList){
            if (type == Layers[obj.@name]){
                return true;
            }
        }
        return false;
    }



回答3:


FlashBuilder autocompletion will give you all the constant on your class and much more.

http://www.adobe.com/products/flashbuilder/



来源:https://stackoverflow.com/questions/3871576/how-can-i-list-all-the-const-properties-defined-in-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!