是否可以针对组件类型和/或继承类型测试对象?

时间:2009-05-18 19:34:35

标签: coldfusion cfc

  

更新:根据答案,我最初选择使用IsInstanceOf()的路线,这是为此需求而设计的。然而,由于某些未知原因,它被证明是非常低效的。在稍后调试应用程序时,我最终只是在要使用的对象上设置一些属性而不是IsInstanceOf,从而导致数量级的速度提升。

我要做的是在ColdFusion中测试一个对象,看看它是什么类型的组件。有点像...

<cfif isValid( "compath.dog", currentObj)>
    ...do something specific with dog objects...
</cfif>

我认为这是可能的,但收到一个错误,说我传递的类型与有效的类型列表中的类型不对应...

  

有效的类型参数是:any,array,Boolean,date,numeric,query,string,struct,UUID,GUID,binary,integer,float,eurodate,time,creditcard,email,ssn,telephone,zipcode,url,正则表达式,范围,组件或变量名称。

有没有办法在ColdFusion中实现这一目标?

4 个答案:

答案 0 :(得分:8)

您可以使用GetMetaData查找类型。一些快速代码:

<cfif GetMetaData(currentObj).type eq "compath.dog">

答案 1 :(得分:7)

您也可以使用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>

答案 2 :(得分:3)

您可以使用getmetadata()函数中的name或fullname。

<cfif GetMetaData(currentObj).name eq "compath.dog">
    ...do something specific with dog objects...
</cfif>

<cfif GetMetaData(currentObj).fullname eq "compath.dog">
    ...do something specific with dog objects...
</cfif>

文档在getmetadata()处于getmetadata()返回的内容,具体取决于对象类型。

答案 3 :(得分:0)

Dan,随意将代码从MXUnit中删除,这完全符合您的需要。我们在assertIsTypeOf()断言中执行此操作。有关详细信息,请参阅此处:http://code.google.com/p/mxunit/source/browse/mxunit/trunk/framework/MXUnitAssertionExtensions.cfc

您看到使用isInstanceOf()获得性能的原因很可能与在Application.cfc中设置this.customTagPaths有关。我自己打了这个并且最近提交了一个bug。希望无论如何都会在CF10中得到修复。

相关问题