Reflection in Actionscript 3.0/Flex 2
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: 在AS3中你也许可以在FLASH.UTIL中找到一个结合的包,这些包提供反射的工具,不要将这里的反射与视觉效果中的“反射”混淆。这里提到的反射符合以下的特点。(相信写过些程序的人肯定知道反射式什么?国外的作者很细心)
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: 你可以象描述那样的使用一个对象来返回例如一个XML类,例如:
package {
importflash.display.Sprite
importflash.utils.describeType
publicclassDescribeTypeExample extendsSprite {
publicfunctionDescribeTypeExample(){
varchild:Sprite = newSprite()
vardescription: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 {
importflash.display.DisplayObject
importflash.display.Sprite
importflash.utils.getDefinitionByName
publicclassGetDefinitionByNameExample extendsSprite {
privatevarbgColor:uint = 0xFFCC00;
privatevarsize:uint = 80
publicfunctionGetDefinitionByNameExample(){
varClassReference:Class= getDefinitionByName(“flash.display.Sprite”)as Class
varinstance:Object= newClassReference()
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: 当然也有些限制,当FLASH PLAYER来处理反射的时候,因为编译时候的信息欠缺,在运行的时候,对于SPRITE这样内建的类可以很顺利,但是如果是自定义的类,在进行下面的一些操作的时候我们可能会遇到麻烦
package {
importcom.customtypes.string// Custom String Implementation Class
importflash.utils.getDefinitionByName
publicclassGetDefinitionByNameExample {
publicfunctionGetDefinitionByNameExample(){
varClassReference:Class= getDefinitionByName(“com.customtypes.string”)as Class
varinstance:Object= newClassReference()
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: 虽然我们导入了需要的包,getDefinitionByName方法调用会因为没有内部的引用而中断,原因在前面也提到过运行时编译在目前还是不被允许的。要解决这个问题,你必须在前述的代码中至少申明了一个对象的示例。这样你的自定义对象就会被编译器编译从而在动态执行的时候获得相关的信息
varcustomType : 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. 因此,如果你有这样的想法:使用反射和XML文件(保存UI的一些配置)动态的创建一些列的UI,你必须实现申明那些你自定义的UI,以便FLASH PLAYER在运行时能够获知这些信息
本文转自
http://cynergysystems.com/blogs/page/keunlee?entry=reflection_in_actionscript_3_0
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: 在AS3中你也许可以在FLASH.UTIL中找到一个结合的包,这些包提供反射的工具,不要将这里的反射与视觉效果中的“反射”混淆。这里提到的反射符合以下的特点。(相信写过些程序的人肯定知道反射式什么?国外的作者很细心)
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: 你可以象描述那样的使用一个对象来返回例如一个XML类,例如:
package {
importflash.display.Sprite
importflash.utils.describeType
publicclassDescribeTypeExample extendsSprite {
publicfunctionDescribeTypeExample(){
varchild:Sprite = newSprite()
vardescription: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 {
importflash.display.DisplayObject
importflash.display.Sprite
importflash.utils.getDefinitionByName
publicclassGetDefinitionByNameExample extendsSprite {
privatevarbgColor:uint = 0xFFCC00;
privatevarsize:uint = 80
publicfunctionGetDefinitionByNameExample(){
varClassReference:Class= getDefinitionByName(“flash.display.Sprite”)as Class
varinstance:Object= newClassReference()
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: 当然也有些限制,当FLASH PLAYER来处理反射的时候,因为编译时候的信息欠缺,在运行的时候,对于SPRITE这样内建的类可以很顺利,但是如果是自定义的类,在进行下面的一些操作的时候我们可能会遇到麻烦
package {
importcom.customtypes.string// Custom String Implementation Class
importflash.utils.getDefinitionByName
publicclassGetDefinitionByNameExample {
publicfunctionGetDefinitionByNameExample(){
varClassReference:Class= getDefinitionByName(“com.customtypes.string”)as Class
varinstance:Object= newClassReference()
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: 虽然我们导入了需要的包,getDefinitionByName方法调用会因为没有内部的引用而中断,原因在前面也提到过运行时编译在目前还是不被允许的。要解决这个问题,你必须在前述的代码中至少申明了一个对象的示例。这样你的自定义对象就会被编译器编译从而在动态执行的时候获得相关的信息
varcustomType : 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. 因此,如果你有这样的想法:使用反射和XML文件(保存UI的一些配置)动态的创建一些列的UI,你必须实现申明那些你自定义的UI,以便FLASH PLAYER在运行时能够获知这些信息
本文转自
http://cynergysystems.com/blogs/page/keunlee?entry=reflection_in_actionscript_3_0