G
Guest
I have two questions;
1) When executing the sub routine "TestStructure", I'm trying to update the
member "intTyres" in the structure "structureCar" dynamically using
System.Reflection. The code executes, but the value does not change, nor is
there an exception thrown.
2) Read comments in sub routine "TestIntegerProperty"
Imports System.Reflection
Imports System.Windows.Forms
Public Class cReflection
Public Structure structureCar
Public intTyres As Integer
Public strName As String
End Structure
Dim mTestStructure As New structureCar
Dim mTestIntegerProperty As New System.Windows.Forms.Button
Public Sub TestStructure()
Dim myType As Type = GetType(structureCar)
Dim myFieldInfo As FieldInfo = myType.GetField("intTyres",
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.InvokeMethod Or
BindingFlags.Instance Or BindingFlags.SetField)
'The next line of code is supposed to change the value of the
"intTyres" member to 5, but it stays zero.
'When a class is used instead of a structure it works. (this is not
an option)
myFieldInfo.SetValue(mTestStructure, 5) 'This does not generate an
exeception
End Sub
Public Sub TestIntegerProperty()
Dim objValue As Object
objValue = "200"
Dim myType As Type = GetType(System.Windows.Forms.Button)
Dim myFieldInfo As PropertyInfo = myType.GetProperty("Left")
'The following line of code does not change the value of the "Left"
property, becuase objValue is seen as string.
'Assigning an integer to objValue does however work, but this is not
an option for us for various reasons. How do I get the method "SetValue" to
accept the object, "objValue", as a string, but convert it to an integer(if a
number is a string, declared as an object, does the object not automatically
get converted to an integer, because the property expects an integer?).
myFieldInfo.SetValue(mTestIntegerProperty, objValue, Nothing)
'Exception gets generated.
'The following doesn't work as well
'I am trying to ctype the "string" objValue to the PropertyType of
the "Left" property
'Uncomment line 41 and 42 to see error
'----------------------------------------------------------------------------------------------------------------------
'Dim tType As Type = Type.GetType(myFieldInfo.PropertyType.FullName)
'How do I create a dynamic type so that "Ctype" can see the type
"tType"
'myFieldInfo.SetValue(mTestIntegerProperty, CType(objValue, tType),
Nothing)
'----------------------------------------------------------------------------------------------------------------------
End Sub
End Class
1) When executing the sub routine "TestStructure", I'm trying to update the
member "intTyres" in the structure "structureCar" dynamically using
System.Reflection. The code executes, but the value does not change, nor is
there an exception thrown.
2) Read comments in sub routine "TestIntegerProperty"
Imports System.Reflection
Imports System.Windows.Forms
Public Class cReflection
Public Structure structureCar
Public intTyres As Integer
Public strName As String
End Structure
Dim mTestStructure As New structureCar
Dim mTestIntegerProperty As New System.Windows.Forms.Button
Public Sub TestStructure()
Dim myType As Type = GetType(structureCar)
Dim myFieldInfo As FieldInfo = myType.GetField("intTyres",
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.InvokeMethod Or
BindingFlags.Instance Or BindingFlags.SetField)
'The next line of code is supposed to change the value of the
"intTyres" member to 5, but it stays zero.
'When a class is used instead of a structure it works. (this is not
an option)
myFieldInfo.SetValue(mTestStructure, 5) 'This does not generate an
exeception
End Sub
Public Sub TestIntegerProperty()
Dim objValue As Object
objValue = "200"
Dim myType As Type = GetType(System.Windows.Forms.Button)
Dim myFieldInfo As PropertyInfo = myType.GetProperty("Left")
'The following line of code does not change the value of the "Left"
property, becuase objValue is seen as string.
'Assigning an integer to objValue does however work, but this is not
an option for us for various reasons. How do I get the method "SetValue" to
accept the object, "objValue", as a string, but convert it to an integer(if a
number is a string, declared as an object, does the object not automatically
get converted to an integer, because the property expects an integer?).
myFieldInfo.SetValue(mTestIntegerProperty, objValue, Nothing)
'Exception gets generated.
'The following doesn't work as well
'I am trying to ctype the "string" objValue to the PropertyType of
the "Left" property
'Uncomment line 41 and 42 to see error
'----------------------------------------------------------------------------------------------------------------------
'Dim tType As Type = Type.GetType(myFieldInfo.PropertyType.FullName)
'How do I create a dynamic type so that "Ctype" can see the type
"tType"
'myFieldInfo.SetValue(mTestIntegerProperty, CType(objValue, tType),
Nothing)
'----------------------------------------------------------------------------------------------------------------------
End Sub
End Class