{"id":278,"date":"2009-05-08T11:28:23","date_gmt":"2009-05-08T18:28:23","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1308"},"modified":"2009-05-08T11:28:23","modified_gmt":"2009-05-08T18:28:23","slug":"loop-through-all-properties-of-a-class","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=278","title":{"rendered":"Loop Through All Properties of a Class"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage {\r\n\t\r\n\timport flash.display.Sprite;\r\n\timport flash.utils.describeType;\r\n\t\r\n\tpublic class Main extends Sprite {\r\n\t\t\r\n\t\tpublic function Main(){\r\n\t\t    var test:Test = new Test();\r\n\t\t\tvar desc:XML= describeType(test);\r\n\t\t\t\/\/ public vars\r\n\t\t\tfor each (var v:XML in desc.variable){\r\n\t\t\t\ttrace(v.@name, test&#x5B;v.@name]);\r\n\t\t\t}\r\n\t\t\t\/\/ getters\r\n\t\t\tfor each (v in desc.accessor){\r\n\t\t\t\ttrace(v.@name, test&#x5B;v.@name]);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t}\r\n}\r\n\r\nclass Test{\r\n\tpublic var a:Number = 123;\r\n\tpublic var b:Number = 100;\t \r\n\tprivate var _getterVal:Boolean = false;\r\n\tpublic function get getter():Boolean{\r\n\t\treturn _getterVal;\r\n\t}\r\n}\r\n\/*\r\noutputs:\r\nb 100\r\na 123\r\ngetter false\r\n*\/\r\n<\/pre>\n<p>I&#8217;m working on a few libraries, <a href=\"https:\/\/actionsnippet.com\/?cat=113\" target=\"blank\">QuickBox2D<\/a> and a library for auto-generated UI stuff&#8230; this technique just came in handy. It shows how to use describeType() to loop through public vars and getters of a given class. <\/p>\n<p>The title of this post should really be Loop Through All PUBLIC properties of a class&#8230;. but it was long enough as it is&#8230;.<br \/>\n<i>Note: this should be run as document class<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>package { import flash.display.Sprite; import flash.utils.describeType; public class Main extends Sprite { public function Main(){ var test:Test = new Test(); var desc:XML= describeType(test); \/\/ public vars for each (var v:XML in desc.variable){ trace(v.@name, test&#x5B;v.@name]); } \/\/ getters for each (v in desc.accessor){ trace(v.@name, test&#x5B;v.@name]); } } } } class Test{ public var a:Number = 123; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,7,2,34],"tags":[3,4],"class_list":["post-278","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-object","category-oop","category-xml","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=278"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/278\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}