FieldInfo.SetValue sets the value of the field supported by the given object. : FieldInfo « Reflection « VB.Net






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

   
  








Related examples in the same category

1.FieldInfo Class represents the attributes of a field and provides access to field metadata.
2.FieldInfo.Attributes Property gets the attributes associated with this field.
3.FieldInfo.FieldHandle Property gets a RuntimeFieldHandle
4.FieldInfo.FieldType Property gets the type of this field object.
5.FieldInfo.GetFieldFromHandle gets a FieldInfo for the field represented by the specified handle.
6.FieldInfo.GetFieldFromHandle
7.FieldInfo.GetValue Method returns the value of a field supported by a given object.
8.FieldInfo Get Value
9.FieldInfo.IsAssembly
10.FieldInfo.IsInitOnly Property tells whether the field can only be set in the body of the constructor.
11.FieldInfo.IsNotSerialized
12.FieldInfo.IsPinvokeImpl
13.FieldInfo.IsPrivate
14.FieldInfo.IsPublic
15.FieldInfo.IsSpecialName tells whether the SpecialName attribute is set in the FieldAttributes enumerator.
16.FieldInfo.IsStatic
17.FieldInfo.MemberType