Reflection in Actionscript 3.0/Flex 2

本文介绍ActionScript 3中使用flash.utils包提供的反射功能,包括确定对象类、获取类信息、创建运行时未知类的实例等。通过示例演示了如何利用describeType和getDefinitionByName函数实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值