flex下的flash.utils类中包含了反射用到的方法,反射就是在运行时动态的得到的类的信息或者动态的创建类,而不是在程序编译时就new好的,flex中的反射还不是很健全,如果要反射的类是flex自带的,那么运行时没啥问题,如果是自己定义的,就必须在程序某处引用一下,否则会报错“Variable … is not defined”, because the Flash Player can’t find it at run time。
解决办法,在外国网站找到的
1. You can import the class and create a dummy variable. The dummy is needed, otherwise the compiler won’t add the class.
import domain.project.MyClass;
...
private var _dummyClass:MyClass;
2. Instead of a variable you can also use the following notation.
import domain.project.MyClass;
...
MyClass;
3. You can also use the Frame metadata tag:
import domain.project.MyClass;
...
[Frame(extraClass="MyClass")]
All these solutions work, but you need to specify the classes you want to use up front.
4. Far more easy, elegant and flexible is to include the path of the class when calling getDefinitionByName. You won’t need to import anything and you don’t have to create dummy vars or use extra metadata tags.
var myClass = getDefinitionByName("domain.project.MyClass") as Class;
接下来的例子演示了ActionScript 3.0中通过调用getDefintionByName函数,动态创建按钮等控件。