Introspection - Loop through all properties in class

I would like to loop through the properties of a class and ideally pull the values from those properties using the properties’ getter.

I’ve tried this:

Using Xojo.Introspection
Dim info As TypeInfo = GetType(childClassObject)
Dim childProperties As PropertyInfo 'As Auto does not fail with auto type'
childProperties = info.Properties
For Each p As PropertyInfoImp In childProperties'or a for each?
  //do what I need here

What am I missing?

This makes no sense:

DatabaseExtensions.DBRecordBase.Create, line 5
Type mismatch error. Expected class Xojo.Introspection.PropertyInfo, but got class Xojo.Introspection.PropertyInfo
childProperties = info.Properties

and this

DatabaseExtensions.DBRecordBase.Create, line 6
Can’t find a type with this name. Did you mean class Xojo.Introspection.PropertyInfoImp?
For Each p As PropertyInfoImp In childProperties’or a for each?

First, info.Properties returns an array so you’d need to declare childProperties as an array.

This code loops through the properties of a PushButton on a Window and adds the names and values of all its String properties to a ListBox:

Using Xojo.Introspection
Dim info As TypeInfo = GetType(PushButton1)
Dim childProperties() As PropertyInfo
childProperties = info.Properties
For Each p As PropertyInfo In childProperties
  If p.PropertyType.FullName = "String" Then
    Dim value As String = p.Value(PushButton1)
    Listbox1.AddRow(p.Name, value)
  End If

@Paul Lefebvre Thanks! I missed the array part!

Followup, I seem to be missing something:
If I need to do some things with the property’s value, is VarType() the right way to go about it? For example, create a column in a database I need to specify a column type.

I’m not sure I understand your question. Do what things?

You can check the type of the property using the code I showed above and use that information to determine an appropriate DB type.

@Paul Lefebvre

I missed that! Thank you.

Is it possible to access the value of a property in an instance by the name of the property as a string?

for example

dim propertyValue as string
value = info.Properties("Name").Value(objectInstance)

Not like that, but since you get back an array of all the properties you can just loop through it and check the Name property to find the one you want.

Gotcha. Thanks! That’s what I was doing before.