2

我正在尝试从已传递到引用另一个对象的对象中的字符串构建方法调用。

通常在调用对象时,我们编写如下代码:

application.stObj.oNewsBusiness.getNews(argumentCollection=local.stArgs);

然而,我所做的是创建了一个包含对象名称、方法名称和参数集合的数组。

<cfscript>
local.stArgs = {};
local.stArgs.nNewsID = 19;
local.stArgs.sAuthor = "John";

local.aData = [];
local.aData[1] = local.stArgs;
local.aData[2] = "stObj.oNewsBusiness";
local.aData[3] = "getNews";
</cfscript>

但是我正在努力将所有这些重新组合为一个方法调用。

使用建议更新但仍有问题

虽然 cfinvoke 似乎适用于:

<cfinvoke component="#application.stObj.oNewsBusiness#" method="#local.sMethod#" argumentcollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>

执行以下操作时它不起作用:

<cfscript>
local.stArgs = local.aData[1];
local.sObject = local.aData[2];
local.sMethod = local.aData[3];
</cfscript>
<cfinvoke component="application.#local.sObject#" method="#local.sMethod#" argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>

它会产生一个错误:

找不到 ColdFusion 组件或接口 application.stObj.oNewsBusiness

4

3 回答 3

7

CFInvoke 通常用于处理动态方法调用。

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7e0a.html

CFInvoke has an argumentcollection attribute so you can pass your arguments in the way you are used to.

于 2012-06-14T13:33:56.537 回答
2

Dan is correct CFInvoke is the way to go

<cfinvoke component="#mycomponentname#" method="get"  arg1="#arg1#" arg2="#arg2#" arg3=..>
于 2012-06-14T13:47:36.237 回答
0
<cfinvoke component="application.#local.sObject#" method="#local.sMethod#"argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>

from your update won't work because there are no # signs around the component variable.

You could do

<cfset local.componentName = "application." & local.sObject>
<cfinvoke component="#local.componentName#" method="#local.sMethod#"argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>

There's probably an inline way of combining application. with the variable on the cfinvoke call, but I don't know off the top of my head.

Edit: Dan Wilson's comment does it better in an inline way.

于 2012-06-14T14:57:59.193 回答