Discussion:
Setting properties via Reflection
Gray, Lee P.
2004-12-29 16:19:09 UTC
Permalink
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
Mattias Sjögren
2004-12-29 16:47:28 UTC
Permalink
Lee,
currentProperty.SetValue(currentProperty, configValue, Nothing)
You shouldn't pass in the PropertyInfo object itself here as the first
argument, but rather the derived object instance you want to modify.



Mattias
--
Mattias Sjögren
***@mvps.org
Gray, Lee P.
2004-12-29 16:53:08 UTC
Permalink
Post by Mattias Sjögren
Lee,
currentProperty.SetValue(currentProperty, configValue, Nothing)
You shouldn't pass in the PropertyInfo object itself here as
the first argument, but rather the derived object instance
you want to modify.
Mattias
Thanks... I just realized that! However, I changed it to

currentProperty.SetValue(Me, configValue, Nothing)

And now I get:

TargetException: Non-static method requires a target.
Gray, Lee P.
2004-12-29 16:58:55 UTC
Permalink
Post by Mattias Sjögren
Post by Mattias Sjögren
Lee,
currentProperty.SetValue(currentProperty, configValue, Nothing)
You shouldn't pass in the PropertyInfo object itself here
as the first
Post by Mattias Sjögren
argument, but rather the derived object instance you want to modify.
Mattias
Thanks... I just realized that! However, I changed it to
currentProperty.SetValue(Me, configValue, Nothing)
TargetException: Non-static method requires a target.
A little more background:

My base class has a Read() method which loops through the derived class'
Properties, matches them up to key names from app.config, and attempts
to populate the properties with the values read from the file.

The derived class simply defines the properties I want to populate from
app.config. After intantiating the derived class, I call its Read()
method. Therefore I should send "Me" to that PropertyInfo.SetValue
method as I showed in my previous post. Is that correct?

Thanks,
Lee
Gray, Lee P.
2004-12-29 18:55:34 UTC
Permalink
Post by Mattias Sjögren
Post by Mattias Sjögren
Post by Mattias Sjögren
Lee,
currentProperty.SetValue(currentProperty, configValue, Nothing)
You shouldn't pass in the PropertyInfo object itself here
as the first
Post by Mattias Sjögren
argument, but rather the derived object instance you want
to modify.
Post by Mattias Sjögren
Post by Mattias Sjögren
Mattias
Thanks... I just realized that! However, I changed it to
currentProperty.SetValue(Me, configValue, Nothing)
TargetException: Non-static method requires a target.
My base class has a Read() method which loops through the
derived class'
Properties, matches them up to key names from app.config, and
attempts to populate the properties with the values read from
the file.
The derived class simply defines the properties I want to
populate from app.config. After intantiating the derived
class, I call its Read() method. Therefore I should send
"Me" to that PropertyInfo.SetValue method as I showed in my
previous post. Is that correct?
To answer my own question: yes, it's correct and it now works, although
I'm not sure what I changed. I must have had a debug.writeline in there
somewhere that was trying to write the value of an object not yet
instantiated.

Continue reading on narkive:
Loading...