By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Anyone experienced with FieldInfo.SetValue

P: n/a
Im trying to use FieldInfo.SetValue to set the values of fields in a
class, the class is using reflection on itself to popluate its own
fields. but i keep getting this error.

Object type cannot be converted to target type

here is code
Dim myFieldInfo() As System.Reflection.FieldInfo
Dim myType As Type = Me.GetType
' Get the type and fields of FieldInfoClass.
myFieldInfo =
myType.GetFields(Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or

' Display the field information of FieldInfoClass.
Dim i As Integer
For i = 0 To myFieldInfo.Length - 1
Dim customatts() As Object =
If customatts.Length > 0 Then
Dim fm As FieldMember = CType(customatts(0),
If htFields.Contains(fm.DbFieldName) Then
myFieldInfo(i).SetValue(me, htFields(fm.DbFieldName))

End If
Next i

the error occurs on SetValue

Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
<jw*****> wrote in message news:11*********************@f14g2000cwb.googlegro
Object type cannot be converted to target type : : If htFields.Contains(fm.DbFieldName) Then
myFieldInfo(i).SetValue(me, htFields(fm.DbFieldName))

It's because the Type retrieved from (Hashtable?) htFields with the
key fm.DbFieldName is not convertible to the Type of the field
whose FieldInfo you're using.

You may need to typecast, using CType( ) or CStr( ) or something,
or the types may just be incompatible (you can't put a String into a
field of type Integer - if the String happens to contain a number you
have to Parse it out manually, as an example).

Look at htFields( fm.DbFieldName).GetType( ) and the type of
the field, and then try to convert between them if possible.
Derek Harmon
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.