I don’t always work with the Flex Framework, so over and over I’ve written the following:
package my.project.package.utils { public class Type { private static const cache:Dictionary = new Dictionary(false); public static function describe(value:Object):XML{}; } }
The describe function caches the results of a call to describeType() based on the type of Class passed in as the Value. But it’s super ugly! All over the place I’ve had to reference a static class: “Type.describe(myObject);” lame.
Why can’t the results of describeType() just be cached in the player? Maybe it is now and I just don’t know it? If so, please tell me!
So here’s the solution if you’d rather have a global function than a static method call:
package org.tinytlf.utils { import flash.utils.*; public function reflect(type:Object, refreshCache:Boolean = false):XML { const typeCache:Dictionary = ReflectionCache.cache; if(!(type is Class)) { if(type is Proxy) type = getDefinitionByName(getQualifiedClassName(type)) as Class; else if(type is Number) type = Number; else type = type.constructor as Class; } if(refreshCache || typeCache[type] == null) typeCache[type] = flash.utils.describeType(type); return typeCache[type]; } } import flash.utils.Dictionary; internal class ReflectionCache { public static const cache:Dictionary = new Dictionary(false); }
An internal class! Yipee! Static, but internalized. Until now I didn’t think you could store a static stateful reference for global functions to use.
Now you can go around all over the place calling “reflect(myObject);”, enjoying the convenience of describeType() and the benefits of using a static method.
Tags: describeType, reflection



Why not, what it si usefull for ?
Are you really sure that Dictionnary lookup is faster than direct describeType function call ?
Maybe should you provide an example ?!
Eric,
The Dictionary lookup is much faster than calling describeType() every time. Documented here and here.
OK thanks.
Did you try AS3 Commons? They have a reflection API with cache built in