looping through object property names in actionscript

我的梦境 提交于 2019-12-11 08:14:36

问题


I have a dynamic class that I have created

public dynamic class SiteZoneFileUploadVO
{       
    public var destination:String = "sitezone";
    public var siteZoneId:uint;
    public var fileType:String;
    public var fileContents:String;

    public function SiteZoneFileUploadVO()
    {
    }

}

when I try to iterate over this object's property names it only iterates the dynamically added properties.

        parameters.dynVar= "value";

        for(var name:String in parameters) 
        {
            trace(name);
        }

Even though the object has all the properties equal to a value (ive checked this in the debugger) the only property name that will be traced is dynVar.

How can I iterate over all the property names and not just the dynamically added ones?


回答1:


You can use describeType() to get an XML with all methods and variables of your class and then filter out the properties you want to iterate over (e.g. all variables) and store them in an XMLList.

As the next step you would then iterate over the XMLList and use square bracket notation on your object to access the filtered properties by their names. However, you can only access public properties this way because describeType() won't look at private properties.




回答2:


If you're running flex:

Looked at a few posts, ObjectUtil.toString was the most promising, then looked at the flex source code for it, it uses another method ObjectUtil.getClassInfo which is exactly what you need. If you just want property names:

ObjectUtil.getClassInfo(myClass).properties

returns an Array of QName objects, each has a localName property which will give you a string for each property name




回答3:


Just use trace(ObjectUtil.toString(parameters)); That should give you your entire object.



来源:https://stackoverflow.com/questions/2694850/looping-through-object-property-names-in-actionscript

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