[英]Given the package, class, function(s), and parameters in Strings, how can I dynamically invoke a function in AS3?
I need to send requests between two separate .swf
s, A
and B
. 我需要在两个独立的
.swf
, A
和B
之间发送请求。 I have an interface
, IRequest
with the following methods: 我有一个
interface
IRequest
具有以下方法:
public interface IRequest {
function get packagePath():String;
function get className():String;
function get functions():Vector.<String>;
function get parameters():Array;
function execute():void;
}
I implement it in a class, Request
. 我在
Request
类中实现它。 One can create a new request as follows: RequestManager.addToQueue(new Request("com.myPackage", "MyClass", "instance.method1.method2", "parameter"));
可以如下创建一个新请求:
RequestManager.addToQueue(new Request("com.myPackage", "MyClass", "instance.method1.method2", "parameter"));
This creates a new Request
with a supplied package
, class
-name, list of functions
and parameters
. 这将使用提供的
package
, class
, list of functions
和parameters
list of functions
创建一个新的Request
。
Things become complicated with a chain of function
-calls and deciding if a function
is actually a function
or property
( public function getMyThing():Object
vs public function get myThing():Object
). 一系列的
function
使事情变得复杂,并确定一个function
实际上是一个function
还是property
( public function getMyThing():Object
与public function get myThing():Object
)。
For example, things are simple enough if the desired Request
is new Request("com.myPackage", "MyClass", "getInstance", "");
例如,如果所需的
Request
是new Request("com.myPackage", "MyClass", "getInstance", "");
事情就足够简单了new Request("com.myPackage", "MyClass", "getInstance", "");
Invoking that method is as simple as: 调用该方法很简单:
var theClass:Class = getDefinitionByName(packagePath + "." + className) as Class;
var theInstance:Object = theClass[functions[0]](); // Invokes com.myPackage.MyClass.getInstance()
However, this won't work if getInstance
is a property
/ getter
. 但是,如果
getInstance
是property
/ getter
,则此方法不起作用。 Also, if there are numerous method calls, I'm not sure how to invoke the chain in one call. 另外,如果有许多方法调用,我不确定如何在一个调用中调用该链。 For example:
例如:
var theClass:Class = getDefinitionByName(packagePath + "." + className) as Class;
var object = theClass[functions[0]]()[functions[1]()[functions[2](); // Invokes com.myPackage.MyClass.firstMethod.secondMethod.thirdMethod()
However, I need to do this dynamically, and I don't see an easy way of dynamically chaining those [
, ]
. 但是,我需要动态地执行此操作,并且看不到动态链接这些
[
, ]
的简便方法。
If anyone knows a nice way for me to be able to invoke methods from a String
, that's essentially what I want to do. 如果有人知道我可以通过
String
调用方法的好方法,那基本上就是我想要做的。
"com.myPackage.MyClass.firstMethod.secondMethod.thirdMethod()", parameters
Thanks for the help, 谢谢您的帮助,
I think doing a check whether that argument actually holds a function before invoking it would help. 我认为在调用该参数之前先检查该参数是否确实具有功能会有所帮助。
var theClass:Class = getDefinitionByName(packagePath + "." + className) as Class;
if(theClass[functions[0]] is Function){
var theInstance:Object = theClass[functions[0]](); // Invokes com.myPackage.MyClass.getInstance()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.