为每个Vo一一书写clone()方法是一件麻烦事,利用flex的反射功能可实现较通用的克隆方法,本方法可保留类型信息,比flex自带的ObjecProxy的copy()和clone()好用哦,代码如下:
??? import flash.utils.ByteArray;
??? import flash.utils.describeType;
??? import flash.utils.getDefinitionByName;
??? import flash.utils.getQualifiedClassName;
???
??? import mx.collections.ArrayCollection;
??? import mx.collections.IList;
??? import mx.formatters.DateFormatter;
??? import mx.utils.ObjectUtil;
???
??? public class CommonUtil extends Object
??? {
??????? public static function clone(obj:Object):Object
??????? {
??????????? var instance:Object = null;
????????????????????
??????????? try {
??????????????? var xmlType:XML = describeType(obj);
??????????????? var typeName:String = xmlType.@name;
??????????????? if(isBasicTypeName(typeName)) return obj;???????????
??????????????? if(typeName == "Array")
??????????????? {
??????????????????? var arr:Array = new Array();
??????????????????? for(var i:String in obj)
??????????????????? {
??????????????????????? arr[i] = clone(obj[i]);
??????????????????? }
??????????????????? return arr;
??????????????? }
??????????????? else if(typeName.search("ArrayCollection") >= 0)
??????????????? {
??????????????????? var coll:ArrayCollection = new ArrayCollection();
??????????????????? for each(var item:Object in obj)
??????????????????? {
??????????????????????? coll.addItem(clone(item));
??????????????????? }
??????????????????? return coll;
??????????????? }
???????????????
??????????????? var className:String = getQualifiedClassName(obj);
??????????????? var clazz:Class = Class(getDefinitionByName(className));
??????????????? instance = new clazz();
???????????????
??????????????? var objInfo:Object = ObjectUtil.getClassInfo(obj);
??????????????? var fieldName:Array = objInfo["properties"] as Array;
??????????????? for each(var q:QName in fieldName){
??????????????????? //trace(q.localName);
??????????????????? if(xmlType.@isDynamic == "true")
??????????????????? {
??????????????????????? instance[q.localName] = clone(obj[q.localName]);
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? var accs:XMLList = xmlType..accessor.(@name == q.localName);
??????????????????????? if(accs != null && accs.length() > 0 && (accs[0].@access == "readwrite" || accs[0].@access == "write"))
??????????????????????? {
??????????????????????????? instance[q.localName] = clone(obj[q.localName]);
??????????????????????? }
??????????????????? }
??????????????? }
??????????? }
??????????? catch(e:ReferenceError)
??????????? {
??????????????? trace(e);
??????????? }
??????????? catch(e:TypeError)
??????????? {
??????????????? trace(e);
??????????? }??
???????????
??????????? return instance;
??????? }
?
??????? public static function isBasicTypeName(typeName:String):Boolean
??????? {
??????????? return (typeName == "String"
??????????????? || typeName == "int"
??????????????? || typeName == "uint"
??????????????? || typeName == "Boolean"
??????????????? || typeName == "Number"
??????????????? || typeName == "null");
??????? }
??? }
使用方法:
public class A????
{??
??? private var _a:int = 1;
??? private var _arr:Array = ['a','b','c'];???
??? public function get a():int
??? {
??????? return _a;
??? }
??? public function set a(value:int):void
??? {
??????? _a = value;
??? }
??? public function get arr():Array
??? {
??????? return _arr;
??? }
??? public function set arr(value:Array):void
??? {
??????? _arr = value;
??? }
}
var a:A = new A();
var b:A = CommonUtil.clone(a);?????? // b是a的一个副本