سؤال

How do you get the full classpath of a static function in Actionscript, when given only the reference to the function? See the below code sample:

public class Tests {
     static function myFunc():void {}
}

var func:Function = Tests.myFunc;

trace(func.?) // should trace "Tests.myFunc"

Is this possible? Even the open source editor and debugger FlashDevelop can't do this. Try rolling over an object with a function ref, it shows the raw pointer (like 12341234) instead of the full name of the function. Of course with describeType you can get info about a type, ie. a class reference, but not when you only have a function ref. Given only a function reference, how do you get its name?

هل كانت مفيدة؟

المحلول

It's not possible in general, having only reference to the function (as mentioned in this accepted answer Actionscript - Obtain the name of the current function). There is only two ways to get the name:

  1. using describeType, but in this case you have to provide the function host object. You can find example in the first answer Actionscript - Obtain the name of the current function, I modified it for the static functions as well and utilizing getSavedThis that works in the debug player only:

    var name:String = getFunctionName( staticTest );
    trace("1", name);
    name = getFunctionName( staticTest, Astest ); //Astest the name of the class that hosts staticTest function
    trace("2", name);
    
    public static function staticTest():void
    {
    
    }
    
    public static function getFunctionName( func:Function, parent:* = null ):String
    {
        if(!parent)
        {
            //works only in debug flash player
            parent = getSavedThis(staticTest);
        }
    
        var methods:XMLList = describeType(parent)..method;
        for each ( var m:XML in methods)
        {
            if (parent[m.@name] == func)
                return m.@name;
        }
        return null;
    }
    

output:

[trace] 1 staticTest
[trace] 2 staticTest
  1. using getStackTrace (example in the same answer) but it works only in debug player and you have to be in the function closure.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top