使用ObjectUtil时,有一个名为toString()的方法接受一个对象。如果你传给一个名为“Person”的类,它将返回字符串“[class Person]”。
var person:Person = new Person();
trace(ObjectUtil.toString(person));//UPDATE I'm not using ObjectUtil.toString()
// traces [class Person]
是否有toObject()方法?采用相同格式toString输出并创建如下实例的东西:
var person:Person = ObjectUtil.toObject("[class Person]");
更新:
抱歉。这是不正确的。我以为我在使用ObjectUtil.toString()。我不是。当我使用该方法时,它返回如下内容:
(com.printui.assets.skins::fontList)#0
accessibilityDescription = ""
accessibilityEnabled = true
accessibilityImplementation = (null)
在我的代码中,它正在返回“[类Person]”,就像我所描述的那样。这是一行:
var currentValue:* = target[property];
popUpValueInput.text = currentValue;
我认为它使用的是instance.toString()但是toString()没有返回任何接近它的内容:
var f:fontList = new fontList();
var f1:fontList = new fontList();
trace("" + f);
trace("" + f1);
trace(f1.toString());
结果:
fontList2
fontList5
fontList5
答案 0 :(得分:1)
一般来说,你应该这样做: 在Person类中添加以下方法:
public function toString():String
{
return "Person" ;
}
因此,要按名称创建类的实例,请使用以下代码:
var p = new (getDefinitionByName( ObjectUtils.toString(person)))
或者它可以在所有类中使用正则表达式(感谢1.21千兆瓦):
var p = new (getDefinitionByName( ObjectUtil.toString(Person).match(/\((.*)\)/)[1] ) );