Page 1 of 1

Finding out a Property's type from it's Class Instance

Posted: Fri Aug 07, 2009 2:59 pm
by ConvertFromOldNGs
by Roland Pope >> Tue, 25 Nov 2008 22:45:29 GMT

Hi all,

I am trying to create a generic update audit mechanism to record changes made to certain objects. When an object is updated by a transaction agent, I go through the properties on the Object being updated and compare them to old values and record the changes.

class :=app.getSchema.getClass("MySchema);
foreach property in class.getProperties() do
//record changes to this property if need be
endforeach;

My question is "Given a 'Property', how do I work out what 'Kind' of property it is? Eg. a String, Boolean, Array etc".

I know can do a 'any := object.getPropertyValue(property.name)' then do an 'any.isKindOf('Type')' check, but this only works if the object in question has a Non-Null value for the property?
So stated another way, "How do I work out a property's type if the instance of the object has a null value for the property?"


Thanks

Roland

Re: Finding out a Property's type from it's Class Instance

Posted: Fri Aug 07, 2009 2:59 pm
by ConvertFromOldNGs
by Dean Cooper >> Tue, 25 Nov 2008 23:06:57 GMT

Every Property object has a "type" reference, inherited from Feature. The "type" will reference an instance of one of the Type subclasses, which tells you the type of the property.

Dean.

Re: Finding out a Property's type from it's Class Instance

Posted: Fri Aug 07, 2009 2:59 pm
by ConvertFromOldNGs
by cnwrjp1 >> Wed, 26 Nov 2008 1:03:36 GMT

Just following this up a little further as I was confused as to how to work out if the property was a Collection or not. Thanks again to Dean Cooper for this answer:

<snip>
The type of the object referenced by "type" is a kind of Type... you can tie yourself up in knots thinking about this! :-)

Collection isn't a kind of Type, because the Collection class doesn't inherit from the Type class.

You need to do something like this:

if prop.type <> null then
if prop.type.isKindOf(PrimType) then
// it's a primitive property
elseif prop.type.isKindOf(CollClass) then
// the property is a collection
write "collection member class = " &
prop.type.CollClass.memberType.name;
elseif prop.type.isKindOf(Class) then
// the property is a non-collection class
else
// the property is an interface reference, you probably want to ignore these
endif;
else
// prop.type is null - shouldn't happen
endif;
</snip>