Just discovered a weird/tricky bug when using Introspection in 2008R1:
Introspection.PropertyInfo.Value will let you assign an object of the
wrong class to an object's property, even if that property is a not a
variant. This seems like a bug to me -- shouldn't RB be able to do
runtime type checking in this case?
I was trying to write a generic "set any object value by name" routine
when I discovered this. It's fairly easy to detect this and prevent
it (simply check that when you are assigning an object that the
TypeInfo.fullName matches)
Here is my revised code (note-this may be buggy, use with caution):
Sub SetPropertyByName(extends o as object, name as string, newVal as
variant)
' given a property name, such as "x" or an object.property name
such as "person.name", sets the value
' recursive!
dim props() as introspection.PropertyInfo =
Introspection.getType(o).GetProperties
dim p as introspection.PropertyInfo
' split a multi-level object name into a chain, e.g.
objectX.objectY.propertyZ
dim chain() as string = split(name, ".")
dim result as variant
for each p in props
if p.name = name then
' type-safety when assigning whole objects
if newVal.Type = Variant.TypeObject then
dim t1 as Introspection.TypeInfo = p.propertyType
dim t2 as Introspection.TypeInfo =
Introspection.getType(newVal.ObjectValue)
if t1.fullName <> t2.fullName then
LogError "SetPropertyByName: object type mismatch : " +
t1.fullname + " <> " + t2.fullName
raise new TypeMismatchException
end if
end if
p.value(o) = newVal
return
elseif p.name = chain(0) then
' return the property value from this object
dim childObject as object = p.value(o)
chain.Remove 0
dim childPropName as string = join(chain,".")
childObject.SetPropertyByName(childPropName, newVal)
return
end if
next
LogError "Object.GetPropertyByName: missing property : " + name
break
End Sub
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|