FiftyOneReasons

Interactive Media Development + Exploration

Iterating Through AS3 Class Properties

Recently, we ran into a situation while building an application in Flash to generate runtime code to be consumed by Flash itself. To make a long story short, we needed a way to iterate through all the properties of any given class – or more specifically, any built in component in Flex. To accomplish this, we stumbled upon a little known reference in the AS language API:

package
{
    import flash.display.Sprite;
    import flash.utils.describeType;
 
    public class IterateThroughClass
    {
        public function IterateThroughClass()
        {
            var child:Sprite = new Sprite();
            var description:XMLList = describeType(child)..variable;
 
            for(var i:int; i < description.length(); i++)
            {
                //trace the name and value
                trace(description[i].@name + ':' + child[description[i].@name]);
            }
        }
    }
}

A very handy utility indeed.

Share the love:
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • StumbleUpon
  • Technorati

Comments

Justin Bowes

Aren’t these instance properties, not class properties? Class properties would be Sprite.someProperty, not (new Sprite()).someProperty as you’ve shown here.

Wayne Helman

Yes Justin, true. To be semantically correct, it would be a class instance. But at runtime, I don’t think there’s a way to iterate through a class without instantiating it first, although admittedly, I haven’t tried.


Leave Your Mark




Feel free to use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">