zwetan

Untitled

Apr 8th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MyFunClass { }
  2.  
  3. var type:Class = MyFunClass;
  4. var bytes:ByteArray = new ByteArray();
  5. bytes.writeObject(type);
  6.  
  7. bytes.position = 0;
  8. var value:* = bytes.readObject();
  9. // value is an Object, not a Class
  10.  
  11. /*
  12. and that's normal
  13. you try to serialize the type instead of the instance
  14.  
  15. ByteArray will not able to write data by reference, only by value
  16.  
  17. so a Class type will not work
  18. you have to provide an instance of the type
  19.  
  20. also for a custom type to be correctly serialized/deserialized by ByteArray
  21.  
  22. you need to implements IExternalizable
  23. and use registerClassAlias()
  24.  
  25. see the code bellow:
  26. */
  27.  
  28. test/MyFunClass.as
  29. -----
  30. package test
  31. {
  32.     import flash.net.registerClassAlias;
  33.     import flash.utils.IDataInput;
  34.     import flash.utils.IDataOutput;
  35.     import flash.utils.IExternalizable;
  36.    
  37.     public class MyFunClass implements IExternalizable
  38.     {
  39.         private var _name:String;
  40.        
  41.         public var test1:Boolean;
  42.         public var test2:Boolean;
  43.        
  44.         public function MyFunClass( name:String = "" )
  45.         {
  46.             _name = name;
  47.         }
  48.        
  49.         public function writeExternal( output:IDataOutput ):void
  50.         {
  51.             output.writeUTF( _name );
  52.             output.writeBoolean( test1 );
  53.             output.writeBoolean( test2 );
  54.         }
  55.  
  56.         public function readExternal( input:IDataInput ):void
  57.         {
  58.             _name = input.readUTF();
  59.             test1 = input.readBoolean();
  60.             test2 = input.readBoolean();
  61.         }
  62.  
  63.         public function toString():String
  64.         {
  65.             return "[MyFunClass " + "\"" + _name + "\" " + "test1:" + test1 + ", test2:" + test2 + "]";
  66.         }
  67.  
  68.     }
  69.    
  70.     registerClassAlias( "test.MyFunClass", MyFunClass );
  71. }
  72. -----
  73.  
  74. main.as
  75. -----
  76. package
  77. {
  78.     import flash.display.Sprite;
  79.     import flash.utils.ByteArray;
  80.     import flash.utils.getQualifiedClassName;
  81.    
  82.     import test.MyFunClass;
  83.  
  84.     public class main extends Sprite
  85.     {
  86.         public function main()
  87.         {
  88.             var value0:MyFunClass = new MyFunClass( "hello world" );
  89.             trace( getQualifiedClassName( value0 ) );
  90.             trace( value0 );
  91.            
  92.             var bytes:ByteArray = new ByteArray();
  93.             bytes.writeObject( value0 );
  94.            
  95.             bytes.position = 0;
  96.             var value1:* = bytes.readObject();
  97.             trace( getQualifiedClassName( value1 ) );
  98.             trace( value1 );
  99.             trace( value1 is MyFunClass );
  100.             trace( value0 == value1 );
  101.            
  102.            
  103.         }
  104.     }
  105. }
  106. -----
Advertisement
Add Comment
Please, Sign In to add comment