Mega Code Archive

 
Categories / VB.Net / Reflection
 

FieldInfo SetValue sets the value of the field supported by the given object

Imports System Imports System.Reflection Imports System.Globalization Public Class Example    Private myString As String    Public Sub New()       myString = "Old value"    End Sub     ReadOnly Property StringProperty() As String       Get          Return myString       End Get    End Property End Class  Public Module FieldInfo_SetValue    Sub Main()         Dim myObject As New Example()         Dim myType As Type = GetType(Example)         Dim myFieldInfo As FieldInfo = myType.GetField("myString",BindingFlags.NonPublic Or BindingFlags.Instance)         Console.WriteLine(myFieldInfo.GetValue(myObject))         ' Change the field value using the SetValue method.          myFieldInfo.SetValue(myObject, "New value")         Console.WriteLine(myFieldInfo.GetValue(myObject))     End Sub  End Module