2012-07-21 20 views
6
static public const CONST_1 :String = "CONST_1"; 
static public const CONST_A :String = "CONST_A"; 

public var constantsArr :Array; 

是否有可能獲取類常量數組,而無需手動添加它們是這樣的:AS3 - 如何獲得一個類的常量數組?

constantsArr = [ CONST_1, CONST_A ]; 
+0

可能重複的http:// stackoverflow.com/questions/3871576/how-can-i-list-all-the-const-properties-defined-in-a-class – loxxy 2012-07-22 04:45:22

回答

14

使用的describeType它應該是可能的:

public class Constants 
{ 
    static public const CONST_1 :String = "CONST_1"; 
    static public const CONST_A :String = "CONST_A"; 
} 

var xmlList:XMLList = describeType(Constants).child("constant"); 

var constantsArray:Array = []; 
for each(var key:XML in xmlList) 
{ 
    constantsArray.push(key.attribute("name")); 
} 
+1

太棒了!非常感謝您的快速回答。 – 2012-07-21 23:09:44

+1

不客氣! – 2012-07-21 23:17:44

相關問題