Is it possible to test an object by component type and / or inherited type?

Update:. Based on the answers, I first went the way of using IsInstanceOf (), which was designed for this need. However, for some unknown reason, it turned out to be extremely ineffective. When debugging the application later, I simply set some object properties to use instead of IsInstanceOf, which led to an improvement in speed by orders of magnitude.

What I'm trying to do is check the object in ColdFusion to find out what type of component it is. Something like...

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

I thought it was possible, but we get an error saying that the type I pass does not match one in a valid type list ...

Valid arguments are: any, array, Boolean, date, numeric, query, string, struct, UUID, GUID, binary, integer, float, eurodate, time, creditcard, email, ssn, telephone, zipcode, url, regex, range, component or variableName.

Is there a way to do this in ColdFusion?

+6
coldfusion cfc
source share
4 answers

You can also use IsInstanceOf (). Although you should still use the full path, you can also use it to determine inheritance or identify components that implement a particular interface.

 <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> 
+7
source share

You can use GetMetaData to search for a type. Quick code:

 <cfif GetMetaData(currentObj).type eq "compath.dog"> 
+8
source share

you can use the name or full name from the getmetadata () function.

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

or

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

docs here getmetadata () that returns getmetadata () depending on the type of object.

+3
source share

Dan, feel free to copy the code from MXUnit, which does exactly what you need to do. We do this in our assertIsTypeOf () statement. See here for more details: http://code.google.com/p/mxunit/source/browse/mxunit/trunk/framework/MXUnitAssertionExtensions.cfc

The reason you see performance improvements with isInstanceOf () is most likely due to the installation of this.customTagPaths in your Application.cfc. I hit it myself and recently filed an error. Hopefully it will be fixed in CF10 if possible.

0
source share

All Articles