Get AS3 instance method reference from class object

class Foo { public function bar():void { ... } } var clazz:Class = Foo; // ...enter the function (no Foo literal here) var fun:Function = clazz["bar"]; // PROBLEM: returns null // later fun.call(new Foo(), ...); 

What is the right way to do this? The explicit equivalent of what I want to do:

 Method m = Foo.class.getMethod("bar", ...); m.invoke(new Foo(), ...); 

Actual code (with workaround):

 class SerClass { public var className:String; public var name:String; private var ser:String = null; private var unser:Function = null; public function SerClass(clazz:Class):void { var type:XML = describeType(clazz); className = type.@name ; // determine name name = type.factory.metadata.(@name=="CompactType").arg.(@key=="name") .@value ; // find unserializer var mdesc:XML = XML(type.method.metadata.(@name=="Unserialize")).parent(); if (mdesc is XML) { unser = clazz[ mdesc.@name ]; } // find serializer var sdesc:XML = XML(type.factory.method.metadata.(@name=="Serialize")).parent(); if (sdesc is XML) { ser = sdesc.@name ; } } public function serialize(obj:Object, ous:ByteArray):void { if (ser == null) throw new Error(name + " is not serializable"); obj[ser](ous); } public function unserialize(ins:ByteArray):Object { if (unser == null) throw new Error(name + " is not unserializable"); return unser.call(null, ins); } } 
+6
source share
4 answers

My workaround is to save the function name instead of the Function object.

 var fun:String = "bar"; // later... new Foo()[fun](...); 
-1
source

Here the functional panel exists only when your class is instantiated:

 var foo:Foo = new Foo() var fun:Function = foo.bar // <-- here you can get the function from the new instance 

if you want to access it directly, you must make it static:

 class Foo { public static function bar():void{ ... } } 

Now you can access your function from the Foo class:

 var fun:Function = Foo.bar 

or

 var clazz:Class = Foo var fun:Function = clazz["bar"] 
+2
source

I'm not sure what you are going to do.

However AS3Commons , particularly reflection package has API, which allows you to work with the methods of class instances and properties.

There are also API methods for instantiating certain types of classes on the fly and calling their respective methods.

Greetings

+1
source

Is not

  fun.call(new Foo(), ...); 

Use instead because for function

no parameters required
 fun.call(clazz); 

The first parameter specified in adobe docs.
An object that sets the value of thisObject inside the body of the function.


[EDIT]
I forgot to indicate that you need to create an instance of a non-static class with the keyword "new".

 var clazz:Class = new Foo(); 

[EDIT2]
Ok, I played and thought I got what you want.

base.as

 package{ public class Base { public function Base() { trace('Base constructor') } public function someFunc( ){ trace('worked'); } } } //called with var b:Base = new Base( );// note I am not type casting to Class var func:Function = b.someFunc; func.call( ); 
0
source

Source: https://habr.com/ru/post/888820/


All Articles