Introspection + obtaining property's value

A couple of thread’s have existed throughout the past years… but can anyone summarize what is the best way to get / set a property value after having obtained it’s name using introspection.

Call the Value method on the PropertyInfo object you get from Introspection:

Dim p() As Introspection.PropertyInfo = Introspection.GetType(THEOBJECT).GetProperties()
p(0).Value(THEOBJECT) = 1
If p(0).Value(THEOBJECT) = 1
1 Like

Thanx Andrew, In the introspection example project that comes with xojo, the property names are easily fetches (as well as attributes etc.) but retrieving the values dynamically … just doesn’t work :confused:

// Get the properties of the class
Var properties() As Introspection.PropertyInfo
properties = info.GetProperties

Var name As String
Var scope As String

For Each p As Introspection.PropertyInfo In properties
  Var cell As String
  cell = name + " As " + p.PropertyType.Name
  
  var sValue as Variant 
  sValue = p.Value(info) // This will not retrieve the value
  
  // This will not work either
  p.Value(info) = "This is not working!"
  IntrospectionList.AddRow(cell)
Next```

What happens when you try? An exception?

1 Like

No exception.

Here is the method, param info as Introspection.TypeInfo
Returns a dictionary with all property names as keys and the corresponding values

The values however do not match. For example:

  • IDE default value : &c4BA84F
  • the default value of property FEATURE_ICON runtime : &hh03BA84F (color.FromString)
  • using p.value(object) returns another value
Var properties() As Introspection.PropertyInfo
properties = info.GetProperties

Var name As String
Var scope As String
var d as new  dictionary

For Each p As Introspection.PropertyInfo In properties
  name = p.Name
  
  // hardcoded for testing purposes
  if name = "FEATURE_ICON" then 
    d.Value(name) =  p.Value(info).ColorValue
    d.Value("realValue") = app.Layout.FEATURE_ICON.ToString
  end if
  
Next

return d

SOLVED!!!

Will share the full code… someone might find it useful.

2 Likes