如何获得变量类型?并实例化它?

时间:2009-07-16 13:47:46

标签: flex air

我正在尝试创建一个打开窗口但确保同一窗口尚未打开的功能。我希望能够传递一个非实例化的var或一个实例化的var,它可以以任何方式工作。如果窗口已经打开,它将关闭它然后重新打开它。

所以我需要一种方法来传递一个Window类型的变量或子类,并实例化正确的子类。

我正在寻找类似的东西:

public function openWindowOnce(window:Window):void
{
    if(isOpen(window))
    {
        closeIfOpen(window);
    }
    window = new Window(); /**<-- THIS LINE window can also be a sublcass of window, 
                             *    I want to instatiate the correct sublass,
                             *    I also want to make sure that it is a Window or a
                             *    Sublcass of window before I instatiate it.
                             */ 
    window.open();
}

谢谢!

1 个答案:

答案 0 :(得分:5)

您可以尝试使用flash.utils.getDefinitionByName()flash.utils.getQualifiedClassName()ClassFactory的组合来获得结果。

var className:string = getQualifiedClassName(object); //returns the class name    
var classObj:Class = getDefinitionByName(className) as Class; //get a Class object
var factory:IFactory = new ClassFactory(classObj);// get a Class factory    
var newObj:Object = factory.newInstance();