2012-11-20 27 views
0

鑑於像如何反思Flex功能的簽名?

function printAndAdd(s: String, a: int, b: int) { 
    // ... 
} 

的功能有沒有辦法在運行時枚舉函數的參數(他們的名字以及他們的類型)?像

for (var arg: ArgumentDescriptor in printAndAdd) { 
    // arg.type yields the class object of the argument, i.e. 'String' or 'int'. 
    // arg.name yields the name of the argument, i.e. 's' or 'a' 
} 

東西我有有不同的簽名的事件處理程序的列表,我也得到了事件處理程序的名稱,以及調用爲對象的Array。我只能將apply()數組添加到函數中,但我想先進行一些錯誤檢查以提供更好的錯誤消息。

回答

2

您可以使用describeType()來查找您尋找的信息。然而,爲了這個工作,功能必須public。私人方法不會被describeType()內省。

假設printAndAddMyClass的方法,你可以這樣做:

你將無法找到
var metadata:XML = describeType(MyClass); 
//find all the 'parameter' nodes of any method called 'printAndAdd' 
var params:XMLList = metadata..method.(@name == "printAndAdd").parameter; 

for each (var param:XML in params) { 
    var index:int = [email protected]; 
    var type:String = [email protected]; 
    var optional:Boolean = [email protected] == "true"; 
} 

一件事,是paramater的名字,但我想它的索引並可滿足您目標。

如果您需要比此更強大的反射,請看as3commons reflect library