Introspection property from class

Is it possible with introspection (or elsewise) to tell which class or subclass a property is coming from?

For example I have 2 Classes
DBClass (super of DBContact)

  • Id as string
  • table as string
    DBContact (subclass of DBClass)
  • Name as string
  • DOB as date

When I introspect the properties on DBContact I want to be able to tell which properties on the DBContact object are coming from DBContact class and which properties are coming from DBClass class.

I want to be able to do this without having to hardcode in the list of properties from the parent class.

I don’t see anything on PropertyInfo that provides that information. Perhaps you could try putting attributes on the properties of the superclass and then checking for that using PropertyInfo.GetAttributes?

In my current work around I just create a new instance of the parent class:

dim ignoreProperties() as String dim n as new DBClass dim parentProperties() as Introspection.PropertyInfo = Introspection.GetType(n).getproperties For i as integer = 0 to parentProperties.Ubound ignoreProperties.Append(parentProperties(i).name) Next

This works slightly better than having to manually add the list of properties but it would still be nice if there was a generic way to do this. For example if I didn’t know what the parent class was.

If I understand what you want correctly this may help to show you how you could do it:

Function PropertyList(val as object) As string()
  dim ti as Introspection.TypeInfo = Introspection.GetType(val)
  dim props() as Introspection.PropertyInfo = ti.GetProperties()
  dim result() as string
  result.Append("Listing properties for: " + ti.FullName)
  for each p as Introspection.PropertyInfo in props
  if ti.BaseType <> nil then
    dim tibase as Introspection.TypeInfo = ti.BaseType
    dim propsbase() as Introspection.PropertyInfo = tibase.GetProperties()
    result.Append(ti.FullName + " is a subclass of " + tibase.FullName)
    for each p as Introspection.PropertyInfo in propsbase
  return result
End Function

Using BaseType you could continue to discover the properties of any other superclasses (by looping or recursive calls if you changed the code), my example above just looks at one level of subclassing.

When I execute the code and display the string array in a simple window for a simple subclass I get:

Listing properties for: SubClass
SubClass is a subclass of SuperClass

So you can see which properties are in both and you didn’t need to know what the superclass (parent class) was.

Interestingly if you override a superclass property in the subclass you get two properties with the same name listed for the subclass

Listing properties for: SubClass
SuperClassProp1 <-------------------------- #1
SuperClassProp1 <-------------------------- #2
SubClass is a subclass of SuperClass

So I guess that is a way to tell if you have re-implemented in the subclass

Hope this helps

Without Introspection:

Just have a Interface/Method called Classname which returns (If Parent <> Nil) Parent.Classname + “.” + LocalClassName (which is a string or constant or so).

This way you even can use more generic names and patterns in names which may not allowed as real ClassNames. Just a thought.