Share via


FieldInfo.FieldHandle Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a handle to the internal metadata representation of a field.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public MustOverride ReadOnly Property FieldHandle As RuntimeFieldHandle
public abstract RuntimeFieldHandle FieldHandle { get; }

Property Value

Type: System.RuntimeFieldHandle
A handle to the internal metadata representation of a field.

Exceptions

Exception Condition
MethodAccessException

This member is invoked late-bound through mechanisms such as Type.InvokeMember.

Remarks

The handles are valid only in the application domain in which they were obtained.

Examples

The following example retrieves field information for MyField, in MyClass, and displays the field associated with the field handle.

Imports System.Reflection

Public Class [MyClass]
   Public MyField As String = "Microsoft"
End Class '[MyClass]

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim [myClass] As New [MyClass]()
      ' Get the type of MyClass.
      Dim myType As Type = GetType([MyClass])
      Try
         ' Get the field information of MyField.
         Dim myFieldInfo As FieldInfo = myType.GetField("MyField", BindingFlags.Public Or BindingFlags.Instance)

         ' Determine whether or not the FieldInfo object is null.
         If Not (myFieldInfo Is Nothing) Then
            ' Get the handle for the field.
            Dim myFieldHandle As RuntimeFieldHandle = myFieldInfo.FieldHandle

            DisplayFieldHandle(outputBlock, myFieldHandle)
         Else
            outputBlock.Text &= "The myFieldInfo object is null." & vbCrLf
         End If
      Catch e As Exception
         outputBlock.Text += String.Format(" Exception: {0}", e.Message.ToString()) & vbCrLf
      End Try
   End Sub 'Main

   Public Shared Sub DisplayFieldHandle(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myFieldHandle As RuntimeFieldHandle)
      ' Get the type from the handle.
      Dim myField As FieldInfo = FieldInfo.GetFieldFromHandle(myFieldHandle)
      ' Display the type.
      outputBlock.Text &= ControlChars.Cr + "Displaying the field from the handle." + ControlChars.Cr & vbCrLf
      outputBlock.Text += String.Format("The type is {0}.", myField.ToString()) & vbCrLf
   End Sub 'DisplayFieldHandle
End Class 'FieldInfo_FieldHandle
using System;
using System.Reflection;

public class MyClass
{
   public string MyField = "Microsoft";
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      MyClass myClass = new MyClass();

      // Get the type of MyClass.
      Type myType = typeof(MyClass);

      try
      {
         // Get the field information of MyField.
         FieldInfo myFieldInfo = myType.GetField("MyField", BindingFlags.Public
             | BindingFlags.Instance);

         // Determine whether or not the FieldInfo object is null.
         if (myFieldInfo != null)
         {
            // Get the handle for the field.
            RuntimeFieldHandle myFieldHandle = myFieldInfo.FieldHandle;

            DisplayFieldHandle(outputBlock, myFieldHandle);
         }
         else
         {
            outputBlock.Text += "The myFieldInfo object is null." + "\n";
         }
      }
      catch (Exception e)
      {
         outputBlock.Text += String.Format("Exception: {0}", e.Message) + "\n";
      }
   }

   public static void DisplayFieldHandle(System.Windows.Controls.TextBlock outputBlock, RuntimeFieldHandle myFieldHandle)
   {
      // Get the type from the handle.
      FieldInfo myField = FieldInfo.GetFieldFromHandle(myFieldHandle);

      // Display the type.
      outputBlock.Text += "\nDisplaying the field from the handle.\n" + "\n";
      outputBlock.Text += String.Format("The type is {0}.", myField.ToString()) + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.