util class 的复制方法无效

copy method of util class is not working

我有一名 class 员工:

public class Employee
{
    public var name:String;
    public function Employee(name:String)
    {
        this.name = name;
    }
}

现在我正在尝试使用 ObjectUtil 创建 ArrayCollection 名员工的副本:

protected function button1_clickHandler(event:MouseEvent):void
{
    var newEmployees = ObjectUtil.copy(employees);
    for each(var emp:Employee in newEmployees) {
        Alert.show(emp.name);
    }

}

但是抛出异常:

Main Thread (Suspended: TypeError: Error #1034: Type Coercion failed: cannot convert Object@ef41a19 to objectutil.Employee.)
objectutil::ObjectUtilCopyCheck/button1_clickHandler
objectutil::ObjectUtilCopyCheck/___ObjectUtilCopyCheck_Button1_click

谁能找出这里出了什么问题? TIA.

方法 copy 在内部使用本机序列化技术进行复制:

public static function copy(value:Object):Object
{
    var buffer:ByteArray = new ByteArray();
    buffer.writeObject(value);
    buffer.position = 0;
    var result:Object = buffer.readObject();
    return result;
}

如果我们使用remoting 标签就很简单了。例如:

//assume employee has [RemoteClass] metadata. 
var newEmployees = Employee (ObjectUtil.copy(emp));

否则你需要将此 class 注册为(假设 com.app.vo.Employee 是 class Employee 的包):

registerClassAlias("com.app.vo.Employee",Employee);
//Now we can copy and caste
var newEmployees = Employee (ObjectUtil.copy(emp));

希望对您有所帮助。