Is it possible to figure out the variable type when the variable isn't instantiated?
Example of what I'm trying to accomplish:
var foo:ExampleOne;
var bar:ExampleTwo;
var arr:Array = [foo, bar];
for each(var myVar:Object in arr开发者_JAVA技巧)
{
myClass = new getDefinitionByName( getQualifiedClassName( myVar ) ) // doesn't work
}
No, because the variables are a reference to objects that are not created in the vm until you create them using the "new" keyword. So until you do this, the references (foo/bar etc) will always be null no matter what operation you're running on them.
I don't know what do you want to do, but you could may be use describeType
:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.ByteArray;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
public class TestTextfield extends Sprite
{
public var foo:TextField;
public var bar:ByteArray;
public function TestTextfield()
{
var arr:Array = ["foo", "bar"];
for each(var myVar:String in arr)
{
var varClass : String = describeType(this)..variable.(@name == myVar).@type.toString();
var myClass : * = new (getDefinitionByName(varClass) as Class)
}
}
}
}
Try to use the "className" property.
It should return "TextInput", "Button", etc... depending the case
for each (var item:* in myArray)
{
if(item.hasProperty('className'))
{
trace("item ["+i+"] is :" + item['className']);
}
}
maybe you want to do something like this ?
var a:Class = MyClassA; //put a reference to a Class object into 'a' variable
var b:Class = MyClassB;
var classes:Array = [a,b];
for each(var classObj:Class in classes){
var objInstance : * = new classObj()
}
精彩评论