Set a properties value using introspection

I’m using introspection to loop all variables of an object, in this case it is ‘anvUse’.
this object contains only boolean variables.
I would like to change the value of some variables.

How can I do that ?

Do you really need introspection? Perhaps a dictionary could be a good alternative?

I believe you can type check otherwise and assign it but i’m not sure introspection is any good.

@Johann_Kienbrandt - You can do this using Introspection.

Var oInfo As Introspection.TypeInfo = Introspection.GetType(src)

For Each oProperty As Introspection.PropertyInfo In oInfo.GetProperties 

  If Not oProperty.CanWrite Then Continue
  Select Case oProperty.Name
  Case "A"
    oProperty.Value(src) = True
  End Select


The docs will give you more information. PropertyInfo — Xojo documentation


my ‘src’ is actually a class object containing only boolean properties.
is it possible to change a properties value ?

like this: oProperty.value(src.value) = true

Have you tried the code I shared to you? It does exactly what you asked for.

Replace src with yours anvUse.

thanks Martin, it works now …
I had a field in the object nvUse which was not a boolean and that created an error.

many thanks

Since PropertyInfo.Value is of type Variant, you can cast the datatype by using e.g. VarType to match only Boolean properties.

I used

If pi.PropertyType.Name = “Boolean” Then …

to find only booleans

1 Like