For a project I needed to clone an object of unknown type. And by clone I mean to create a new instance of that same type and then fill out all its properties (including getters and setters) to mirror the original object.
Thanks to a friend, I discovered the describeType function in AS3. But this alone will only take care of the copying part. To create an object of the same type as another one we use getDefinitionByName.
Although Flash reflection is pretty basic, with a little work it will do the trick.
Get the application files.
Here's the code:
< ?xml version="1.0" encoding="utf-8"?>
<mx :Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" creationComplete="init()">
</mx><mx :Script>
< ![CDATA[
import mx.controls.Alert;
private var source:DataObject = new DataObject();
private var cloneObject:DataObject;
private function init():void {
source.name = 'John Doe';
source.howMany = 4.5;
source.when = new Date(0);
source.complexProp = new DataObject();
source.complexProp.name = 'Name in sub-object';
cloneObject = UtilFunctions.clone(source) as DataObject;
Alert.show("Clone:\nname = " + cloneObject.name + "\nhowMany = " + cloneObject.howMany + "\nwhen = " + cloneObject.when + "\ncomplexProp.name = " + cloneObject.complexProp.name);
}
/**
* describeType will produce this (for a DataObject instance):
*
* <type name="DataObject" base="Object" isDynamic="false" isFinal="false" isStatic="false">
<extendsclass type="Object"/>
<accessor name="isHandicap" access="writeonly" type="Boolean" declaredBy="DataObject"/>
<variable name="howMany" type="Number"/>
<accessor name="complexProp" access="readwrite" type="DataObject" declaredBy="DataObject"/>
<variable name="name" type="String"/>
<variable name="when" type="Date"/>
*
* */
]]>
</mx>
And the UtilFunctions.as file:
package
{
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
public class UtilFunctions
{
public static function newSibling(sourceObj:Object):* {
if(sourceObj) {
var objSibling:*;
try {
var classOfSourceObj:Class = getDefinitionByName(getQualifiedClassName(sourceObj)) as Class;
objSibling = new classOfSourceObj();
}
catch(e:Object) {}
return objSibling;
}
return null;
}
public static function clone(source:Object):Object {
var clone:Object;
if(source) {
clone = newSibling(source);
if(clone) {
copyData(source, clone);
}
}
return clone;
}
public static function copyData(source:Object, destination:Object):void {
//copies data from commonly named properties and getter/setter pairs
if((source) && (destination)) {
try {
var sourceInfo:XML = describeType(source);
var prop:XML;
for each(prop in sourceInfo.variable) {
if(destination.hasOwnProperty(prop.@name)) {
destination[prop.@name] = source[prop.@name];
}
}
for each(prop in sourceInfo.accessor) {
if(prop.@access == "readwrite") {
if(destination.hasOwnProperty(prop.@name)) {
destination[prop.@name] = source[prop.@name];
}
}
}
}
catch (err:Object) {
;
}
}
}
}
}
文章摘自:
http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/
分享到:
相关推荐
使用网上流传的一个程序实现pdf截图为png,需要使用Imagic扩展,安装后出现Trying to clone an uncloneable object of class Imagic提示,下面是具体的解决方法分享。
在windows下安装完后提示:Fatal error: Trying to clone an uncloneable object of class Imagick in C:\www\hx\pdf_to_png.php on line 17 使用IIS和Apache均会有这个提示。经多次测试后,发现两种解决方法: 1....
下面小编就为大家带来一篇深入理解JavaScript中的对象复制(Object Clone)。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Getting Started with OpenCart Module Development gives you step-by-step explanations and illustrations on how to clone, customize, and develop modules and pages with OpenCart. This book shows you how...
How To Run Rapid Clone (adcfgclone.pl) Non-Interactively (Doc ID 375650.1)
Clone Detection in Secure Messaging- Improving Post-Compromise Security in Practice
It's a good start to understand how to write mixin in Object-C. ### Import ObjCMixin ```objc #import ``` ### Define and implement a module Declare a module. ```objc @module(MyModule) @property...
Caffe2 Windows third_party(git clone失败或速度慢时可用)
Copy or duplicate an item Lock and unlock an item Edit a field in the Experience Editor Edit the website content Manage associated content Delete an item Edit the layout of an item Reset the ...
scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling. scrollSpeed: 10, // px setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) { data...
Use Syngress' proven "How to Cheat" methodology to configure and build VMware's ESX Server version 3. This clear, concise guide provides all the information you need to become a virtual whiz! This ...
发表在NDSS‘16上的论How to Make ASLR Win the Clone Wars: Runtime Re-Randomization。这篇文章提出了一个RuntimeASLR的机制,让fork()出来的子进程内存空间地址重新随机化。 Background 在Apache,Nignx和OpenSSH...
It is also a common practice to discover exploits and vulnerabilities in existing software. However, it is a manually intensive and time-consuming process even for experienced reverse engineers. An ...
Jlink-clone解决办法,替换文件
几何-战争-克隆 用 actionscript 3 air (starling + AGAL) 编写的几何战争克隆
系统环境:CentOS Linux release 7.6.1810 (Core) 起因:npm构建时报错 ... fatal: unable to access 'https://github.com/nhn/raphael.git/': Failed connect to github.com:443; Connection timed out npm
本人用的JLINK仿真器.在使用新版KEIL时,提示要升级固件,升级后就出现JLINK is Clone的提示。在网上找了许多关于修复的资料,都觉得不是很好。经过本人反复试验,总算找到比较好的解决方案,操作步骤如下