FieldInfo.FieldHandle Property gets a RuntimeFieldHandle
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
Public Class [MyClass]
Public MyField As String = "MyField"
End Class
Public Class FieldInfo_FieldHandle
Public Shared Sub Main()
Dim [myClass] As New [MyClass]()
Dim myType As Type = GetType([MyClass])
Try
Dim myFieldInfo As FieldInfo = myType.GetField("MyField", BindingFlags.Public Or BindingFlags.Instance)
If Not (myFieldInfo Is Nothing) Then
Dim myFieldHandle As RuntimeFieldHandle = myFieldInfo.FieldHandle
DisplayFieldHandle(myFieldHandle)
Else
Console.WriteLine("The myFieldInfo object is null.")
End If
Catch e As Exception
Console.WriteLine(" Exception: {0}", e.Message.ToString())
End Try
End Sub
Public Shared Sub DisplayFieldHandle(ByVal myFieldHandle As RuntimeFieldHandle)
Dim myField As FieldInfo = FieldInfo.GetFieldFromHandle(myFieldHandle)
Console.WriteLine(ControlChars.Cr + "Displaying the field from the handle." + ControlChars.Cr)
Console.WriteLine("The type is {0}.", myField.ToString())
End Sub
End Class
Related examples in the same category