反射机制的研究

在某些情况下需要通过配置文件来获取类名进而进行实例化。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="init()">
    <mx:Script>
        <![CDATA[
             import flash.utils.getDefinitionByName;                   
            private function init():void
            {                
                var ins2:MyTestComponent;// custom class, this can’t be missed, if you don’t have such a sentence,  the flex can’t complie the class into swf file, then runtime error will occur,I don’t know whether there is some solution

                var obj:Object = getDefinitionByName("GCComponent.MyTestComponent"); //as Class;
                 var ClassReference:Class = obj as Class;
                var ins:Object = new ClassReference();
                ins.width = 100;
                ins.height = 10;
                ins.x = 0;
                ins.y = 0;
                this.addChild(ins as UIComponent);        
            }
            
            ]]>
    </mx:Script>  

</mx:Application>

posted @ 2011-03-08 20:40  lyqandgdp  阅读(142)  评论(0编辑  收藏  举报