Share via


Type.GetField Method (String, BindingFlags)

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

Searches for the specified field, using the specified binding constraints.

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

Syntax

'Declaration
Public MustOverride Function GetField ( _
    name As String, _
    bindingAttr As BindingFlags _
) As FieldInfo
public abstract FieldInfo GetField(
    string name,
    BindingFlags bindingAttr
)

Parameters

Return Value

Type: System.Reflection.FieldInfo
A FieldInfo object representing the field that matches the specified requirements, if found; otherwise, nulla null reference (Nothing in Visual Basic).

Implements

IReflect.GetField(String, BindingFlags)

Exceptions

Exception Condition
ArgumentNullException

name is nulla null reference (Nothing in Visual Basic).

Remarks

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

Member Type

Static

Non-Static

Constructor

No

No

Field

No

Yes. A field is always hide-by-name-and-signature.

Event

Not applicable

The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.

Method

No

Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.

Nested Type

No

No

Property

Not applicable

The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.

Notes:

  1. Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.

  2. For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. Custom attributes are not part of the common type system.

The following BindingFlags filter flags can be used to define which fields to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • Specify BindingFlags.Public to include public fields in the search.

  • Specify BindingFlags.NonPublic to include non-public fields (that is, private and protected fields) in the search.

  • Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.IgnoreCase to ignore the case of name.

  • BindingFlags.DeclaredOnly to search only the fields declared on the Type, not fields that were simply inherited.

See System.Reflection.BindingFlags for more information.

If the current Type represents a constructed generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.

If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the fields of the class constraint.

Examples

The following example gets the Type object for the specified class, obtains the FieldInfo object for the field that matches the specified binding flags, and displays the value of the field.


Imports System.Reflection

Public Class MyFieldClassA
   Public Field As String = "A Field"
End Class 

Public Class MyFieldClassB
   Private myField As String = "B Field"

   Public Property Field() As String
      Get
         Return myField
      End Get
      Set(ByVal Value As String)
         If myField <> Value Then
            myField = Value
         End If
      End Set
   End Property
End Class 


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myFieldObjectB As New MyFieldClassB()
      Dim myFieldObjectA As New MyFieldClassA()

      Dim myTypeA As Type = GetType(MyFieldClassA)
      Dim myFieldInfo As FieldInfo = myTypeA.GetField("Field")

      Dim myTypeB As Type = GetType(MyFieldClassB)
      Dim myFieldInfo1 As FieldInfo = myTypeB.GetField("myField", _
          BindingFlags.NonPublic Or BindingFlags.Instance)

      outputBlock.Text += String.Format("The value of the public field is: '{0}'", _
          myFieldInfo.GetValue(myFieldObjectA)) + vbCrLf

      Try
         ' In Silverlight, the value of a private field cannot be accessed
         ' by using reflection.
         outputBlock.Text += String.Format("The value of the private field is: '{0}'", _
            myFieldInfo1.GetValue(myFieldObjectB)) + vbCrLf
      Catch ex As Exception
         outputBlock.Text &= ex.GetType().Name & " occurred: " & ex.Message
      End Try
   End Sub 

End Class 

' This code produces output similar to the following:
'
'The value of the public field is: 'A Field'
'FieldAccessException occurred: SilverlightApplication.MyFieldClassB.myField

using System;
using System.Reflection;

public class MyFieldClassA
{
   public string Field = "A Field";
}

public class MyFieldClassB
{
   private string field = "B Field";
   public string Field
   {
      get
      {
         return field;
      }
      set
      {
         if (field != value)
         {
            field = value;
         }
      }
   }
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      MyFieldClassB myFieldObjectB = new MyFieldClassB();
      MyFieldClassA myFieldObjectA = new MyFieldClassA();

      Type myTypeA = typeof(MyFieldClassA);
      FieldInfo myFieldInfo = myTypeA.GetField("Field");

      Type myTypeB = typeof(MyFieldClassB);
      FieldInfo myFieldInfo1 = myTypeB.GetField("field",
          BindingFlags.NonPublic | BindingFlags.Instance);

      outputBlock.Text += String.Format("The value of the public field is: '{0}'\n",
          myFieldInfo.GetValue(myFieldObjectA));

      try
      {
         // In Silverlight, the value of a private field cannot be accessed
         // by using reflection.
         outputBlock.Text += String.Format("The value of the private field is: '{0}'\n",
            myFieldInfo1.GetValue(myFieldObjectB));
      }
      catch (Exception ex)
      {
         outputBlock.Text += ex.GetType().Name + " occurred: " + ex.Message;
      }
   }
}

/* This code produces output similar to the following:

The value of the public field is: 'A Field'
FieldAccessException occurred: MyFieldClassB.myField
 */

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.