I have two questions;
1) When executing the sub routine "TestStructure" , I'm trying to update the
member "intTyres" in the structure "structureC ar" dynamically using
System.Reflecti on. The code executes, but the value does not change, nor is
there an exception thrown.
2) Read comments in sub routine "TestIntegerPro perty"
Imports System.Reflecti on
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 mTestIntegerPro perty As New System.Windows. Forms.Button
Public Sub TestStructure()
Dim myType As Type = GetType(structu reCar)
Dim myFieldInfo As FieldInfo = myType.GetField ("intTyres",
BindingFlags.No nPublic Or BindingFlags.Pu blic Or BindingFlags.In vokeMethod Or
BindingFlags.In stance Or BindingFlags.Se tField)
'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.Set Value(mTestStru cture, 5) 'This does not generate an
exeception
End Sub
Public Sub TestIntegerProp erty()
Dim objValue As Object
objValue = "200"
Dim myType As Type = GetType(System. Windows.Forms.B utton)
Dim myFieldInfo As PropertyInfo = myType.GetPrope rty("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.Set Value(mTestInte gerProperty, 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(my FieldInfo.Prope rtyType.FullNam e)
'How do I create a dynamic type so that "Ctype" can see the type
"tType"
'myFieldInfo.Se tValue(mTestInt egerProperty, 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 "structureC ar" dynamically using
System.Reflecti on. The code executes, but the value does not change, nor is
there an exception thrown.
2) Read comments in sub routine "TestIntegerPro perty"
Imports System.Reflecti on
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 mTestIntegerPro perty As New System.Windows. Forms.Button
Public Sub TestStructure()
Dim myType As Type = GetType(structu reCar)
Dim myFieldInfo As FieldInfo = myType.GetField ("intTyres",
BindingFlags.No nPublic Or BindingFlags.Pu blic Or BindingFlags.In vokeMethod Or
BindingFlags.In stance Or BindingFlags.Se tField)
'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.Set Value(mTestStru cture, 5) 'This does not generate an
exeception
End Sub
Public Sub TestIntegerProp erty()
Dim objValue As Object
objValue = "200"
Dim myType As Type = GetType(System. Windows.Forms.B utton)
Dim myFieldInfo As PropertyInfo = myType.GetPrope rty("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.Set Value(mTestInte gerProperty, 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(my FieldInfo.Prope rtyType.FullNam e)
'How do I create a dynamic type so that "Ctype" can see the type
"tType"
'myFieldInfo.Se tValue(mTestInt egerProperty, CType(objValue, tType),
Nothing)
'----------------------------------------------------------------------------------------------------------------------
End Sub
End Class
Comment