Extended ByteArray cloning issue

I extended the ByteArray class, for example:

[RemoteClass(alias="MyByteArray")]
public class MyByteArray extends ByteArray {}

      

and cloned an instance of this class using ByteArray # readObject () / writeObject (). However, for some reason, the cloned object is a ByteArray instance and not MyByteArray. This is shown in the following example:

registerClassAlias("MyByteArray", MyByteArray);
var b1:MyByteArray = new MyByteArray();
var tmp:ByteArray = new ByteArray();
tmp.writeObject(b1);           
tmp.position = 0;
var b2:* = tmp.readObject();
trace( b2 is MyByteArray ); // prints false
trace( b2 is ByteArray ); // prints true

      

Also, when I add some custom fields to the MyByteArray class, they are not saved with writeObject () and are not cloned ...

Can someone explain to me why the cloned object is not an instance of MyByteArray?

Thank you very much!

0


a source to share


2 answers


I think this is because tmp is of type ByteArray and not of type MyByteArray. If you do a tmp of type MyByteArray it should return an object of the same type using writeObject I guess? Or you might want the type.



+1


a source


ByteArray is most likely seen as a special case for object serialization, so I doubt you will get around it.



Trying to subclass ByteArray sounds like a code smell to me. I would look at using encapsulation rather than inheritance to solve your problem.

0


a source







All Articles