获取对象的所有属性名称

发布于 2024-12-27 12:01:06 字数 347 浏览 3 评论 0原文

假设我有一个如下所示的类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2025-01-03 12:01:06

for..in 循环仅枚举动态添加的属性。声明的变量和类的方法不会在 for..in 循环中枚举。这意味着 ActionScript API 中的大多数类不会在 for..in 循环中显示任何属性。

如需解决方案,请阅读:

http://livedocs.adobe。 com/flex/3/html/help.html?content=usingas_8.html

A for..in loop enumerates only dynamically added properties. Declared variables and methods of classes are not enumerated in for..in loops. This means that most classes in the ActionScript API will not display any properties in a for..in loop.

For a solution read:

http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html

呆° 2025-01-03 12:01:06

您需要使用 AS 反射/内省。
本机方法是使用describeType函数,如下所示:(

public function getDetails():void { 
        var classInfo:XML = describeType(button1);

        // List the class name.
        ta1.text = "Class " + [email protected]() + "\n";

        // List the object's variables, their values, and their types.
        for each (var v:XML in classInfo..variable) {
            ta1.text += "Variable " + v.@name + "=" + button1[v.@name] + 
                " (" + v.@type + ")\n";
        }
    }

来自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:

public function getDetails():void { 
        var classInfo:XML = describeType(button1);

        // List the class name.
        ta1.text = "Class " + [email protected]() + "\n";

        // List the object's variables, their values, and their types.
        for each (var v:XML in classInfo..variable) {
            ta1.text += "Variable " + v.@name + "=" + button1[v.@name] + 
                " (" + v.@type + ")\n";
        }
    }

(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文