获取对象的所有属性名称
假设我有一个如下所示的类:
public class MyClass
{
public var attribute1;
public var attribute2;
}
并且我想将 attribute1 和 attribute2 作为字符串获取。我尝试了这个:
var test:MyClass = new MyClass();
for (var key:String in test)
{
trace(test[key]);
}
但它不起作用,它永远不会进入循环。我怎样才能做我想做的事?
谢谢
Let's say I have a class which looks like this :
public class MyClass
{
public var attribute1;
public var attribute2;
}
and I'd like to get attribute1 and attribute2 as strings. I tried this :
var test:MyClass = new MyClass();
for (var key:String in test)
{
trace(test[key]);
}
but it does not work, it never goes in the loop. How can I do what I want to do ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如需解决方案,请阅读:
http://livedocs.adobe。 com/flex/3/html/help.html?content=usingas_8.html
For a solution read:
http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html
您需要使用 AS 反射/内省。
本机方法是使用describeType函数,如下所示:(
来自flex文档:http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html)
我不确定,但我认为 mx.utils.ObjectUtil 类可以使就更简单了。而且它仍然是原生方式。
另一种选择是使用库来使其更容易。
看这个: http://www.as3commons.org/as3-commons-reflect /简介.html
You'll need to use AS reflection/introspection.
The native way is using describeType function, like this:
(from flex doc: http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html)
I'm not sure but I think the class mx.utils.ObjectUtil can make it simpler. And it's still native way.
Another option is using a library to make it easier.
Look this one: http://www.as3commons.org/as3-commons-reflect/introduction.html