Mega Code Archive

 
Categories / VB.Net / Reflection
 

FieldInfo Get Value

Imports System Imports System.Reflection Imports Microsoft.VisualBasic Public Class MyClass1     Public myFieldA As String     Public myFieldB As String     Public Sub New()         myFieldA = "A public field"         myFieldB = "Another public field"     End Sub 'New End Class 'MyClass1 Public Class FieldInfo_GetValue     Public Shared Sub Main()         Dim myInstance As New MyClass1()         ' Get the type of MyClass1.         Dim myType As Type = GetType(MyClass1)         Try             Dim myFields As FieldInfo() = myType.GetFields((BindingFlags.Public Or BindingFlags.Instance))             Console.WriteLine(ControlChars.NewLine & "Displaying the values of the fields of {0}." & ControlChars.NewLine, myType.ToString())             Dim i As Integer             For i = 0 To myFields.Length - 1                 Console.WriteLine("The value of the field {0} is: {1}", myFields(i).Name, myFields(i).GetValue(myInstance))             Next i         Catch e As Exception             Console.WriteLine("Exception : {0}", e.Message.ToString())         End Try     End Sub  End Class