Copying flex objects

Source: Internet
Author: User

1. How to Use bytearray to copy objects:

/** <Br/> * copyobject <br/> * @ Param * ob <br/> * @ return * <br/> ***/< br/> Public static function copyobject (OB: *): * <br/>{< br/> var bytedata: bytearray = new bytearray (); </P> <p> bytedata. writeobject (OB); <br/> bytedata. position = 0; </P> <p> return bytedata. readobject (); <br/>}

 

2. How to store and extract more complex types through bytearray:

(That is
Custom object for serialization/deserialization
)

Package <br/> {<br/> public class user <br/> {<br/> private VaR _ firstname: string; <br/> private VaR _ lastname: string; </P> <p> public function set firstname (firstname: string): void <br/>{< br/> _ firstname = firstname; <br/>}</P> <p> public function set lastname (lastname: string): void <br/>{< br/> _ lastname = lastname; <br/>}</P> <p> Public Function get firstname (): String <br/>{< br/> return _ firstname; <br/>}</P> <p> Public Function get lastname (): String <br/>{< br/> return _ lastname; <br/>}< br/>}

 

Store the user instance in binary format to the server or to the local mongodobject. if you try to store the user instance in bytearray and extract it again later, when you read it, flash player will internally retrieve whether the user type has been registered, if no player exists
The object is deserialized according to the object type.

 

VaR User: User = new user (); <br/> User. name = "Dante"; <br/> User. age = 25; <br/> // out put: false <br/> trace (commonutil. copyobject (User) is user );
 

 

In the preceding example, false is output.
We need to use "registerclassalias" to inform Flash Player and register the user type so that flash player can automatically deserialize the user type instances.

 

Registerclassalias ("usertypealias", user); <br/> var User: User = new user (); <br/> User. name = "Dante"; <br/> User. age = 25; <br/> var item: User = commonutil. copyobject (User); <br/> trace (item. name, item. age );

 

PS:
Note that some basic types cannot be serialized or deserialized into AMF. For example, displayobject. Therefore, if you want to serialize A movieclip instance, it will not work.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.