Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyFunClass { }
- var type:Class = MyFunClass;
- var bytes:ByteArray = new ByteArray();
- bytes.writeObject(type);
- bytes.position = 0;
- var value:* = bytes.readObject();
- // value is an Object, not a Class
- /*
- and that's normal
- you try to serialize the type instead of the instance
- ByteArray will not able to write data by reference, only by value
- so a Class type will not work
- you have to provide an instance of the type
- also for a custom type to be correctly serialized/deserialized by ByteArray
- you need to implements IExternalizable
- and use registerClassAlias()
- see the code bellow:
- */
- test/MyFunClass.as
- -----
- package test
- {
- import flash.net.registerClassAlias;
- import flash.utils.IDataInput;
- import flash.utils.IDataOutput;
- import flash.utils.IExternalizable;
- public class MyFunClass implements IExternalizable
- {
- private var _name:String;
- public var test1:Boolean;
- public var test2:Boolean;
- public function MyFunClass( name:String = "" )
- {
- _name = name;
- }
- public function writeExternal( output:IDataOutput ):void
- {
- output.writeUTF( _name );
- output.writeBoolean( test1 );
- output.writeBoolean( test2 );
- }
- public function readExternal( input:IDataInput ):void
- {
- _name = input.readUTF();
- test1 = input.readBoolean();
- test2 = input.readBoolean();
- }
- public function toString():String
- {
- return "[MyFunClass " + "\"" + _name + "\" " + "test1:" + test1 + ", test2:" + test2 + "]";
- }
- }
- registerClassAlias( "test.MyFunClass", MyFunClass );
- }
- -----
- main.as
- -----
- package
- {
- import flash.display.Sprite;
- import flash.utils.ByteArray;
- import flash.utils.getQualifiedClassName;
- import test.MyFunClass;
- public class main extends Sprite
- {
- public function main()
- {
- var value0:MyFunClass = new MyFunClass( "hello world" );
- trace( getQualifiedClassName( value0 ) );
- trace( value0 );
- var bytes:ByteArray = new ByteArray();
- bytes.writeObject( value0 );
- bytes.position = 0;
- var value1:* = bytes.readObject();
- trace( getQualifiedClassName( value1 ) );
- trace( value1 );
- trace( value1 is MyFunClass );
- trace( value0 == value1 );
- }
- }
- }
- -----
Advertisement
Add Comment
Please, Sign In to add comment