C
Charles Law
I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is also a minor change in
class Mypropertyinfo, which I have commented out.
When using a structure, however, the second call to GetValue returns
"Default caption". Can anyone tell me why, and how I can make this work?
<code>
Imports System
Imports System.Reflection
Public Structure MyStructure
Private myCaption As String
Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> Value Then
myCaption = Value
End If
End Set
End Property
End Structure
Class Mypropertyinfo
Public Shared Function Main() As Integer
Console.WriteLine("Reflection.PropertyInfo")
''Dim af As New MyProperty ' original
Dim af As MyStructure
af.Caption = "Default caption"
' Get the type and PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo = MyType.GetProperty("Caption")
' Get and display the GetValue method
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())
' Use the SetValue method to change the caption
Mypropertyinfo.SetValue(af, "This caption has been changed.",
Nothing)
' Get the caption again and display it
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())
Console.ReadLine()
Return 0
End Function
End Class
</code>
TIA
Charles
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is also a minor change in
class Mypropertyinfo, which I have commented out.
When using a structure, however, the second call to GetValue returns
"Default caption". Can anyone tell me why, and how I can make this work?
<code>
Imports System
Imports System.Reflection
Public Structure MyStructure
Private myCaption As String
Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> Value Then
myCaption = Value
End If
End Set
End Property
End Structure
Class Mypropertyinfo
Public Shared Function Main() As Integer
Console.WriteLine("Reflection.PropertyInfo")
''Dim af As New MyProperty ' original
Dim af As MyStructure
af.Caption = "Default caption"
' Get the type and PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo = MyType.GetProperty("Caption")
' Get and display the GetValue method
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())
' Use the SetValue method to change the caption
Mypropertyinfo.SetValue(af, "This caption has been changed.",
Nothing)
' Get the caption again and display it
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())
Console.ReadLine()
Return 0
End Function
End Class
</code>
TIA
Charles