现在的位置: 首页 > 综合 > 正文

Reflection in Actionscript 3.0/Flex 2

2013年05月17日 ⁄ 综合 ⁄ 共 3218字 ⁄ 字号 评论关闭

In actionscript 3, you may find a set of functions in the “flash.utils” package that provides some facilities for reflection. Do not confuse this with the type of reflection you may get when applied as a visual effect, but the type of reflection that does the following basic things:

  • Determine the class of an object.
  • Get information about a class’s modifiers, fields, methods, constructors, and superclasses.
  • Find out what constants and method declarations belong to an interface.
  • Create an instance of a class whose name is not known until runtime.
  • Get and set the value of an object’s field, even if the field name is unknown to your program until runtime.
  • Invoke a method on an object, even if the method is not known until runtime.

You can make use of the functions like “describeType”, which returns an XML object that describes the ActionScript object named as the parameter of the method. Some example uses of this are as followed:

package {
    import flash.display.Sprite;
    import flash.utils.describeType;
   
    public class DescribeTypeExample extends Sprite {
        public function DescribeTypeExample() {
            var child:Sprite = new Sprite();
            var description:XML = describeType(child);
            trace(description..accessor.@name.toXMLString());
        }
    }
}

If we wanted to go to the next step and create objects dynamically by name at runtime, we could manage by using the method “getDefinitionByName()”.

package {
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    public class GetDefinitionByNameExample extends Sprite {
        private var bgColor:uint = 0xFFCC00;
        private var size:uint = 80;

        public function GetDefinitionByNameExample() {
            var ClassReference:Class = getDefinitionByName(“flash.display.Sprite”) as Class;
            var instance:Object = new ClassReference();
            instance.graphics.beginFill(bgColor);
            instance.graphics.drawRect(0, 0, size, size);
            instance.graphics.endFill();
            addChild(DisplayObject(instance));
        }
    }
}

So, Even though these may seem like nifty functions, there are some limitations when it comes to reflection in the flash player, because of the absence of dynamic source compilation at runtime. The above works absolutely great for objects and classes that are already available internally at runtime such as the “Sprite” class. However, for custom class objects, we run into trouble if we do something like the following:

package {
    import com.customtypes.string; // Custom String Implementation Class
    import flash.utils.getDefinitionByName;

    public class GetDefinitionByNameExample {
        public function GetDefinitionByNameExample() {
            var ClassReference:Class = getDefinitionByName(“com.customtypes.string”) as Class;
            var instance:Object = new ClassReference();
            instance.customParameter = “my parameter”;
        }
    }
}

Even with the import statement on “com.customtypes.string”, the method call to “getDefinitionByName” will break without an internal reference to the class. The reason for this is because as stated before, runtime compilation of source is NOT allowable at the current time. Maybe in the future it might, but not now. To get around this, you have to have atleast one instantiation of the class type in your code for the above to work. So in your class definition you might want to declare a variable of the custom type you want to use:

var customType : com.customtypes.string;

So for the idea of dynamically creating a set of views in Flex 2 using these methods in conjuction with an xml file that may hold the names of the views you want to use, in order for that to work, you will have to instantiate a variable of each type of view that you want to utilize.

抱歉!评论已关闭.