Loop Through All Properties of a Class

Actionscript:
  1. package {
  2.    
  3.     import flash.display.Sprite;
  4.     import flash.utils.describeType;
  5.    
  6.     public class Main extends Sprite {
  7.        
  8.         public function Main(){
  9.             var test:Test = new Test();
  10.             var desc:XML= describeType(test);
  11.             // public vars
  12.             for each (var v:XML in desc.variable){
  13.                 trace(v.@name, test[v.@name]);
  14.             }
  15.             // getters
  16.             for each (v in desc.accessor){
  17.                 trace(v.@name, test[v.@name]);
  18.             }
  19.         }
  20.        
  21.     }
  22. }
  23.  
  24. class Test{
  25.     public var a:Number = 123;
  26.     public var b:Number = 100;   
  27.     private var _getterVal:Boolean = false;
  28.     public function get getter():Boolean{
  29.         return _getterVal;
  30.     }
  31. }
  32. /*
  33. outputs:
  34. b 100
  35. a 123
  36. getter false
  37. */

I'm working on a few libraries, QuickBox2D and a library for auto-generated UI stuff... this technique just came in handy. It shows how to use describeType() to loop through public vars and getters of a given class.

The title of this post should really be Loop Through All PUBLIC properties of a class.... but it was long enough as it is....
Note: this should be run as document class

This entry was posted in OOP, Object, XML, dynamic and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. steve
    Posted November 5, 2010 at 2:54 pm | Permalink

    Hi,

    I’ve an issue on my side.
    In the context below, getter = null…
    Have you an idea why ?

    class AbstractTest
    {
    public function toString():String
    {
    var xml:XML = describeType(this);

    var properties:String = _getProperties(xml);
    var accessors:String = _getAccessors(xml);

    var s:String = xml.@name;
    s+= ” | ” + properties + ” | ” + accessors;
    return s;
    }

    private function _getProperties(xml:XML):String
    {
    var aProperties:XMLList = xml.variable.@name;
    var s:String = “Properties -> “;
    for(var i:String in aProperties)
    {
    s+= aProperties[i] + ” = “+ this[aProperties[i]] +” / “;
    }
    return s;
    }

    private function _getAccessors(xml:XML):String
    {
    var aAccessors:XMLList = xml.accessor.@name;
    var s:String = “Accessors -> “;

    for(var i:String in aAccessors)
    {
    s+= aAccessors[i] + ” = “+ this[aAccessors[i]] +” / “;
    }
    return s;
    }
    }

    class TestBase
    {
    public function get getter():Boolean
    {
    return _getterVal;
    }
    }

    class Test extends TestBase
    {
    public var a:Number = 123;
    public var b:Number = 100;
    }

  2. steve
    Posted November 5, 2010 at 2:57 pm | Permalink

    I forgot to mention that TestBase extends AbstractTest, of course

  3. steve
    Posted November 5, 2010 at 3:14 pm | Permalink

    Forget it !
    Getter returns the correct variable, but when I pass value to the constructor, getter variable was not initialized…
    Shame on me !

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*