欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Flex反射工具类

程序员文章站 2022-05-19 14:21:43
...

原文->http://blog.csdn.net/arjick/article/details/6278116

基于Flex的反射工具类,在平常用的比较少,但是在使用反射机制填充AS对象属性等时候还是很有用的。

package com.shine.framework.core.util  
{  
    import flash.utils.describeType;  
    import flash.utils.getDefinitionByName;  
    import flash.utils.getQualifiedClassName;    
    import flash.utils.getQualifiedSuperclassName;    
    import mx.utils.NameUtil;  
      
    import mx.collections.ArrayCollection;  
    import mx.controls.Alert;  
      
    public class ReferenceUtil  
    {  
        public function ReferenceUtil()  
        {  
        }  
          
        /** 
         * 获取class的object package::className 
         * */  
        public static function referenceClass(className:String):Object{  
            var ClassReference:Class = getDefinitionByName(className) as Class;  
            var instance:Object = new ClassReference();  
            return instance;  
        }  
          
        /** 
         * 获取class的value 
         * */  
        public static function referenceClassParameters(className:String,parameters:String):*{  
            return referenceClass(className)[parameters];  
        }  
          
        /** 
         * 执行方法 
         * */  
        public static function referenceClassFunction(className:String,functionName:String,... rest):*{  
            var instance:Object =referenceClass(className);  
            return instance[functionName].apply(instance,rest);   
        }  
          
        /** 
         * 执行方法 
         * */  
        public static function referenceObjectFunction(instance:Object,functionName:String,... rest):*{  
            return instance[functionName].apply(instance,rest);   
        }  
          
        /** 
         * 获取class name全名 
         * */  
        public static function getClassFullName(value:Object):String{  
            return getQualifiedClassName(value);  
        }  
          
          
        /** 
         * 获取class name 
         * */  
        public static function getClassName(value:Object):String{  
            if(getQualifiedClassName(value).indexOf("::")!=-1)  
                return getQualifiedClassName(value).substring(getQualifiedClassName(value).indexOf("::")+2);  
            else  
                return getClassFullName(value);  
        }  
          
        /** 
         * 获取所有的function列表 
         * ps:只能获取去方法的名称,不可以获取protected and private 
         * */  
        public static function getAllFunctionFromObject(instance:Object):ArrayCollection{  
            var functionArrayCollection:ArrayCollection=new ArrayCollection;  
            var method:XMLList=describeType(instance).method;  
            var num:int=method.length();  
            for(var i:int=0;i<num;i++){  
                var xml:XML=method[i];  
                functionArrayCollection.addItem([email protected]);  
                xml=null;  
            }  
            return functionArrayCollection;  
        }  
    }  
}