Attempting to SetValue in FieldInfo Reflection class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am attempting to set a value through the FieldInfo and the value does not
appear to be stored. Here is a snippet of the code:

'This is the Base Class
Imports System.Reflection

Public Class DummyBase

Protected moElements As Object

Public Sub New()

End Sub

Public Overridable Sub MoveToGlobal()
Dim oFieldType As System.Type
Dim value As Object

Dim myFieldInfo() As FieldInfo
Dim myType As Type = moElements.GetType
' Get the type and fields of FieldInfoClass.
myFieldInfo = myType.GetFields(BindingFlags.NonPublic Or _
BindingFlags.Instance Or BindingFlags.Public)

oFieldType = myFieldInfo(0).FieldType
value = 9
Select Case oFieldType.Name
Case "Int32"
myFieldInfo(0).SetValue(moElements, CType(value, Int32))
End Select
End Sub
End Class

'This is the inherited class
Public Class Dummy
Inherits DummyBase

Public Structure Elements
Dim DatabaseID As Int32
End Structure

Public TableElements As Elements

Public Sub New()

MyBase.New()
TableElements.DatabaseID = 10
moElements = TableElements
End Sub

End Class

'This is the class for Testing
Public Class Test

Public Sub New()
Dim DatabaseID As Integer
Dim oDummy As New Dummy
oDummy.MoveToGlobal()
DatabaseID = oDummy.TableElements.DatabaseID
End Sub
End Class


The value is never 9. Right after I perform a SetValue, I do a GetValue in
the Command Window and it stills says 10. The DatabaseID is also still 10.
 
Bruce,

That's because Elements is a Structure, and you end up modifying a
boxed copy of TableElements. It would probably work better of Elements
was a Class.



Mattias
 
Back
Top