metadata动态绑定回调

package avmplus
{
    /**
     * Makes the hidden, inofficial function describeTypeJSON available outside of the avmplus
     * package.
     *
     * <strong>As Adobe doen't officially support this method and it is only visible to client
     * code by accident, it should only ever be used with runtime-detection and automatic fallback
     * on describeType.</strong>
     *
     * @see http://www.tillschneidereit.de/2009/11/22/improved-reflection-support-in-flash-player-10-1/
     */
    public class DescribeTypeJSON
    {
        //----------------------              Public Properties             ----------------------//
        public static var available : Boolean = describeTypeJSON != null;
        
        public static const INSTANCE_FLAGS:uint = INCLUDE_BASES | INCLUDE_INTERFACES
            | INCLUDE_VARIABLES | INCLUDE_ACCESSORS | INCLUDE_METHODS | INCLUDE_METADATA
            | INCLUDE_CONSTRUCTOR | INCLUDE_TRAITS | USE_ITRAITS | HIDE_OBJECT;
        public static const CLASS_FLAGS:uint = INCLUDE_INTERFACES | INCLUDE_VARIABLES
            | INCLUDE_ACCESSORS | INCLUDE_METHODS | INCLUDE_METADATA | INCLUDE_TRAITS | HIDE_OBJECT;
        
        public static const INCLUDE_BASES:uint = avmplus.INCLUDE_BASES;
        public static const INCLUDE_INTERFACES:uint = avmplus.INCLUDE_INTERFACES;
        public static const INCLUDE_VARIABLES:uint = avmplus.INCLUDE_VARIABLES;
        public static const INCLUDE_ACCESSORS:uint = avmplus.INCLUDE_ACCESSORS;
        public static const INCLUDE_METHODS:uint = avmplus.INCLUDE_METHODS;
        public static const INCLUDE_METADATA:uint = avmplus.INCLUDE_METADATA;
        public static const INCLUDE_CONSTRUCTOR:uint = avmplus.INCLUDE_CONSTRUCTOR;
        public static const INCLUDE_TRAITS:uint = avmplus.INCLUDE_TRAITS;
        public static const INCLUDE_ITRAITS:uint = avmplus.USE_ITRAITS;
        public static const HIDE_OBJECT:uint = avmplus.HIDE_OBJECT;
        
        
        //----------------------               Public Methods               ----------------------//
        public function DescribeTypeJSON()
        {
        }
        public function describeType(target : Class, flags : uint) : Object
        {
            return describeTypeJSON(target, flags);
        }
        
        public function getInstanceDescription(type : Class) : Object
        {
            return describeTypeJSON(type, INSTANCE_FLAGS);
        }
        
        public function getClassDescription(type : Class) : Object
        {
            return describeTypeJSON(type, CLASS_FLAGS);
        }
    }
}


//实现代码

package
{
    import flash.display.Sprite;
    import flash.utils.Dictionary;

    public class coco extends Sprite
    {
        /**
         *回调键值对 
         */    
        private var dic:Dictionary = new Dictionary();
        
        public function coco()
        {
            //初始化回调类
            var s:Service = new Service();
            
            //注册回调类
            RegClass.inject(s,dic);
            
            //执行回调类
            for each(var n:Function in dic)
            {
                n.call(null,"sdadas");
            }
        }
    }
}
import avmplus.DescribeTypeJSON;

import flash.utils.Dictionary;
/**
 *注册机
 * @author jiangjq
 */
class RegClass
{
    private static var describer:DescribeTypeJSON=new DescribeTypeJSON();
    public static function inject(target:Object,dic:Dictionary):void
    {
        var ref:Class=target.constructor;
        var describeData:Object=describer.describeType(ref, DescribeTypeJSON.INSTANCE_FLAGS);
        var traits:Object=describeData.traits;
        var itm:Object;
        var meta:Object;
        var metaname:String;
        var mValue:Object;
        for each (itm in traits.methods)
        {
            var func:Function=target[itm.name];
            var obj:Object = itm;
//            trace("===============================",func);
            for each (meta in itm.metadata)
            {
                metaname=meta.name;
//                trace(metaname);
                if(metaname == "CMD")
                {
                    for each (mValue in meta.value)
                    {
                        //                    if (mValue.key == CODE)
                        //                    {
                        //                        code=int(mValue.value);
                        //                    }
    //                    trace(mValue.key,mValue.value);
                        
                        if(mValue.key == "code"){
                            //绑定函数回调
                            dic[mValue.value] = func;
                        }
                    }
                }
            }
        }
    }
}

class Service
{
    public function Service()
    {
    }
    
    [CMD(code=10100)]
    public function submitCallback(data:Object):void
    {
        trace("ParseData: [" + data + "]");
    }
    public function show():void
    {
        
    }
}

 遍历server对象 中的函数属性 字段,对符合要求的函数 存在使用列表中,这样实现对server协议回调的注册。优点是将server协议接口封装在各个模块中.

posted @ 2016-05-06 11:38  泥潭里的金鱼  阅读(311)  评论(0)    收藏  举报