Gray, Lee P.
2004-12-29 16:19:09 UTC
I have a custom class with Property names that match key names in
app.config. Using Reflection in the base class, I loop through the
derived class' properties and attempt to set their values to any
matching keys found in app.config. Everything works fine except
PropertyInfo.SetValue.
.SetValue keeps throwing a TargetException ("Object does not match
target type"). The Debug statement shows that my two values are not
null and are of the same type. Obviously I'm missing what
TargetException actually means. Can anyone help?
' currentProperty is a valid PropertyInfo object at this point
Try
' Attempt to set the found property's value
If currentProperty.CanWrite Then
Debug.WriteLine("Attempting to set " &
currentProperty.PropertyType.Name & _
" property """ & currentProperty.Name & """ to "
& configValue.GetType.Name & _
" value """ & configValue.ToString & """")
currentProperty.SetValue(currentProperty, configValue,
Nothing)
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
'Throw ex
End Try
Here's some of my output:
...
Attempting to set Int32 property "MaxDropDownItems" to Int32 value "4"
Object does not match target type.
Attempting to set Boolean property "IsLocalOnly" to Boolean value "True"
Object does not match target type.
...
Thanks,
Lee
app.config. Using Reflection in the base class, I loop through the
derived class' properties and attempt to set their values to any
matching keys found in app.config. Everything works fine except
PropertyInfo.SetValue.
.SetValue keeps throwing a TargetException ("Object does not match
target type"). The Debug statement shows that my two values are not
null and are of the same type. Obviously I'm missing what
TargetException actually means. Can anyone help?
' currentProperty is a valid PropertyInfo object at this point
Try
' Attempt to set the found property's value
If currentProperty.CanWrite Then
Debug.WriteLine("Attempting to set " &
currentProperty.PropertyType.Name & _
" property """ & currentProperty.Name & """ to "
& configValue.GetType.Name & _
" value """ & configValue.ToString & """")
currentProperty.SetValue(currentProperty, configValue,
Nothing)
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
'Throw ex
End Try
Here's some of my output:
...
Attempting to set Int32 property "MaxDropDownItems" to Int32 value "4"
Object does not match target type.
Attempting to set Boolean property "IsLocalOnly" to Boolean value "True"
Object does not match target type.
...
Thanks,
Lee