2009-07-30 20 views
3

オブジェクトの各メンバーを反復しようとしています。各メンバーについて、それが機能であるかどうかをチェックします。それが関数であれば、その名前を取得し、その関数の名前に基づいていくつかの論理を実行したいと思います。私もこれが可能かどうかわからない。それは...ですか?任意のヒント?Actionscript 3イントロスペクション - 関数名

例:

var mems: Object = getMemberNames(obj, true); 

for each(mem: Object in members) { 
    if(!(mem is Function)) 
     continue; 

    var func: Function = Function(mem); 

    //I want something like this: 
    if(func.getName().startsWith("xxxx")) { 
     func.call(...); 
    } 

} 

私は苦労これを行うに多くを見つけることを抱えています。助けてくれてありがとう。

答えて

4

あなたの擬似コードは、あなたが望むことに近いです。ただし、プライベートメソッドを取得できるgetMemberNamesを使用する代わりに、単純なfor..inループでメンバーをループし、角かっこを使用してメンバーの値を取得することができます。たとえば:

public function callxxxxMethods(o:Object):void 
{ 
    for(var name:String in o) 
    { 
    if(!(o[name] is Function)) 
     continue; 
    if(name.startsWith("xxxx")) 
    { 
     o[name].call(...); 
    } 
    } 
} 
+1

キャスティングは非常に高価であるとして、あなたはまた、「です」または「と」のようなものを使用して、あなたがチェックしているオブジェクトをキャストすることは避けてください - この例がそうであるようはるかに良いです。 – quoo

+0

私が間違っている場合は私を修正しますが、私はあなたが機能にキャストできるとは思わない。 –

+0

@Tyler Egeto:確かに、私は信じていますが、すぐに呼び出すことはできません:Function(o [name])()、あなたはそれを保存する必要があります:var foo:Function = Function(o [name] ); foo(); –

0

Dan Monegoの回答はお金になりますが、動的メンバーの場合のみ機能します。任意の固定のインスタンス(または静的)メンバーについて、あなたはflash.utils.describeTypeを使用する必要があります:ダンの答えと一緒にこの

var description:XML = describeType(obj); 

/* By using E4X, we can use a sort of lamdba like statement to find the members 
* that match our criteria. In this case, we make sure the name starts with "xxx". 
*/ 
var methodNames:XMLList = description..method.(@name.substr(0, 3) == "xxx"); 

for each (var method:XML in methodNames) 
{ 
    var callback:Function = obj[[email protected]]; 
    callback(); // For calling with an unknown set of parameters, use callback.apply 
} 

使用をあなたはダイナミックで固定メンバーが混在している場合。

0

私はいくつかの作業を行い、両方の方法を組み合わせました。気になるのは、一般に公開されているメンバーに対してのみ機能します。それ以外の場合はnullが返されます。

/** 
    * Returns the name of a function. The function must be <b>publicly</b> visible, 
    * otherwise nothing will be found and <code>null</code> returned.</br>Namespaces like 
    * <code>internal</code>, <code>protected</code>, <code>private</code>, etc. cannot 
    * be accessed by this method. 
    * 
    * @param f The function you want to get the name of. 
    * 
    * @return The name of the function or <code>null</code> if no match was found.</br> 
    *   In that case it is likely that the function is declared 
    *   in the <code>private</code> namespace. 
    **/ 
    public static function getFunctionName(f:Function):String 
    { 
     // get the object that contains the function (this of f) 
     var t:Object = getSavedThis(f); 

     // get all methods contained 
     var methods:XMLList = describeType(t)[email protected]; 

     for each (var m:String in methods) 
     { 
      // return the method name if the thisObject of f (t) 
      // has a property by that name 
      // that is not null (null = doesn't exist) and 
      // is strictly equal to the function we search the name of 
      if (t.hasOwnProperty(m) && t[m] != null && t[m] === f) return m;    
     } 
     // if we arrive here, we haven't found anything... 
     // maybe the function is declared in the private namespace? 
     return null;           
    } 

greetz、

TOX

+1

getSavedThis(f)は何ですか? –