Ale*_*exP 5 coldfusion cfc coldfusion-9
我正在尝试找出<cfscript>在ColdFusion 9中调用动态方法的正确语法.我已经尝试了许多变体并进行了很好的搜索.
<cfinvoke>显然是我想要的标签,遗憾的是,我不能在我的纯cfscript组件中使用它,因为它是在ColdFusion 10中实现的.
我在我的CFC中尝试了以下内容:
/** Validate the method name **/
var resources = getResources();
if (structKeyExists(variables.resources, name)) {
variables.resourceActive[name] = true;
var reflectionMethod = resources[name];
var result = "#reflectionMethod.getMethodName()#"(argumentCollection = params);
}
Run Code Online (Sandbox Code Playgroud)
返回值reflectionMethod.getMethodName()是我想要调用的方法名称.它100%返回正确定义和访问该方法的正确值(方法名称),
我的错误是该行的语法错误.
Ada*_*ron 14
您不想获取方法名称,您希望获得实际方法,例如:
function getMethod(string method){
return variables[method];
}
Run Code Online (Sandbox Code Playgroud)
通话,因此:
theMethod = getMethod(variableHoldingMethodName);
result = theMethod();
Run Code Online (Sandbox Code Playgroud)
不幸的是,不能简单地做到这一点:
result = getMethod(variableFoldingMethodName)();
Run Code Online (Sandbox Code Playgroud)
要么:
result = myObject[variableFoldingMethodName]();
Run Code Online (Sandbox Code Playgroud)
由于CF解析器不喜欢括号或括号的加倍.
我建议的方法的警告是它将方法拉出CFC,因此它将在调用代码的上下文中运行,而不是在CFC实例中运行.根据方法中的代码,这可能会也可能不重要.
另一种方法是在对象中注入静态命名的方法,例如:
dynamicName = "foo"; // for example
myObject.staticName = myObject[dynamicName];
result = myObject.staticName(); // is actually calling foo();
Run Code Online (Sandbox Code Playgroud)