Posted At : February 6, 2008 9:00 PM
11 Comments
However in AS3 this code will not work as expected (assuming that MyVO is not a dynamic class). I've come across this a couple of times in the last couple of weeks a yesterday found the solution to it.
There is a new function which gives all the introspection data for classes in AS3 - flash.util.describeType() which returns an XML Object.
To do the previous looping over the properties it could be used as follows:
There is one other thing to catch you out though. If you make your variables [Bindable] or use getters & setters they will not show up as "variable". They will show up instead as "accessor" which is something to be aware of.
Hope it helps. Cheers, Mark
11 Comments
You could probably do something like this to catch getters, setters and bindable properties too:
var def : XML = describeType(obj);
var properties : XMLList = def..variable.@name + def..accessor.@name;
for each ( var property : String in properties ) {
trace(property + ": " + obj[property]);
}
I haven't compiled nor tested the above, but I have done something similar a few times.
Hi Theo,
Thanks for the tip - I tried it out and it works great. I'm getting to like E4X more the more I use it.
Cheers,
Mark
muchas gracias! exactly what I was looking for.
Thanks for this. I was ripping out my hair to try and figure out what was up with AS3 and the ability to loop through my objects properties.
This did the trick and worked perfectly! Thanks for posting these!
Hi there, just stumbled across this . great stuff helped me alot.
The race outputs them vars in my class but not in the order they are weritten, is there a way to keep them in order ?
Hi Joe,
I didn't figure out if there is a way to get them in the order in which they are declared. I'm not sure if the compiled code retains any information on this - but I really don't know.
Cheers,
Mark
It is highly unlikely that the compiled code retains any information about the order in which properties were declared. It has no significance to the running application, and should it happen to exist any hint of the order I'd say it's purely coincidental and could change.
Mark and Theo
Thanks a lot guys! was knocking my head as i couldn't loop through a TextFormat Object i declared, using for in looping. But found that it works in AS2.
My GOD man! Why is everything in AS3 so difficult - there goes 4 hours trying to list my variables! I think I officially HATE AS3. Thanks for the livesaver!
here's your methods combined into a single static method that can inspect an object with either degree of detail. I put it into a class called System.
Example:
System.debug_object(my_class_instance, false);
public static function debug_object(o:Object, show_all:Boolean):void {
import flash.utils.*;
var def:XML = describeType(o);
var props:XMLList = def..variable.@name;
if(show_all) props += def..accessor.@name;
for each (var prop:String in props) {
trace(prop + ": " + o[prop]);
}
}
again, great work guys! I can't believe this isn't more readily available!