开发者

Saving bytecode of a Class (to SharedObject or database)

NOTE: I want to save the actual bytecode for the AS3 class, NOT an instance of it!

A class is being loaded in externally and extracted via a URLLoader and then Loader.loadBytes. A single Class is then extracted out of that Application Domain and used in the project.

Now, rather than saving the entire Loader (either开发者_运维知识库 via SharedObject or other means of storage) is it possible to "extract out" the bytecode of the needed class and all dependencies, which can then be "injected" back into an empty shell or something to be reused when needed?


So you would like to eval(some code)? There is no eval() in AS3, but this might help you: http://danielmclaren.com/2008/09/21/eval-in-as3-tips-for-executing-dynamic-actionscript

You can also save class in AMF like suggested by 3Devil.

Example:

package vos
{
    import flash.utils.ByteArray;
    import org.flixel.FlxPath;

    public class ActionVO extends Object
    {
        public var pid:uint;
        public var cid:uint;
        public var action:String;
        public var data:*;

    }
}

And the class you are saving shared object you need

registerClassAlias( "org.flixel::FlxPath", FlxPath );
registerClassAlias( "org.flixel::FlxPoint", FlxPoint );
registerClassAlias( "VO::ActionVO", ActionVO );

and saving:

var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(data);
shared.data.byteArray = byteArray;

..and loading:

var ba:ByteArray = shared.data.byteArray
ba.uncompress();
var vo:ActionVO = ba.readObject() as ActionVO; 

And there you have vo (simple value object class, including more complex class FlxPath)


You can try using ByteArray.writeObject, which will save a reversable snapshot of object properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜