更新:根据答案我最初使用IsInstanceOf()的路线,这是为这个需要而设计的.然而,由于某些未知原因,它被证明是非常低效的.在稍后调试应用程序时,我最终只是在要使用的对象上设置一些属性而不是IsInstanceOf,从而导致数量级的速度提升.
我想要做的是测试ColdFusion中的一个对象,看看它是什么类型的组件.就像是...
<cfif isValid( "compath.dog", currentObj)>
...do something specific with dog objects...
</cfif>
Run Code Online (Sandbox Code Playgroud)
我认为这是可能的,但收到一个错误,说我传递的类型与有效的类型列表中的类型不对应...
有效的类型参数是:any,array,Boolean,date,numeric,query,string,struct,UUID,GUID,binary,integer,float,eurodate,time,creditcard,email,ssn,telephone,zipcode,url,regex,range ,component或variableName.
有没有办法在ColdFusion中实现这一目标?
您可以使用GetMetaData来查找类型.一些快速代码:
<cfif GetMetaData(currentObj).type eq "compath.dog">
Run Code Online (Sandbox Code Playgroud)
您也可以使用IsInstanceOf().虽然您仍必须使用完整路径,但它也可用于确定继承或识别实现特定接口的组件.
<cfif IsInstanceOf(obj, "compath.Dog")>
yes. it is a dog component {woof}
<cfelse>
some other type of component
</cfif>
<cfif IsInstanceOf(obj, "compath.AnimalInterface")>
yes. it implements the animal interface
<cfelse>
no. it must be vegetable or mineral ...
</cfif>
Run Code Online (Sandbox Code Playgroud)