FieldInfo Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Discovers the attributes of a field and provides access to field metadata.

Inheritance Hierarchy

System..::.Object
System.Reflection..::.MemberInfo
System.Reflection..::.FieldInfo

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

Syntax

Public MustInherit Class FieldInfo _
    Inherits MemberInfo
public abstract class FieldInfo : MemberInfo

The FieldInfo type exposes the following members.

Properties

Name Description
Attributes Gets the attributes that are associated with this field.
CustomAttributes Gets a collection that contains this member's custom attributes. (Inherited from MemberInfo.)
DeclaringType Gets the class that declares this member. (Inherited from MemberInfo.)
FieldHandle Gets a handle to the internal metadata representation of a field.
FieldType Gets the type of this field object.
IsAssembly Gets a value that indicates whether the potential visibility of this field is described by FieldAttributes..::.Assembly; that is, the field is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly.
IsFamily Gets a value that indicates whether the visibility of this field is described by FieldAttributes..::.Family; that is, the field is visible only within its class and derived classes.
IsFamilyAndAssembly Gets a value that indicates whether the visibility of this field is described by FieldAttributes..::.FamANDAssem; that is, the field can be accessed from derived classes, but only if they are in the same assembly.
IsFamilyOrAssembly Gets a value that indicates whether the potential visibility of this field is described by FieldAttributes..::.FamORAssem; that is, the field can be accessed by derived classes wherever they are, and by classes in the same assembly.
IsInitOnly Gets a value that indicates whether the field can be set only in the body of the constructor.
IsLiteral Gets a value that indicates whether the value is written at compile time and cannot be changed.
IsNotSerialized Gets a value that indicates whether this field has the NotSerialized attribute.
IsPinvokeImpl Gets a value that indicates whether the corresponding PinvokeImpl attribute is set in FieldAttributes.
IsPrivate Gets a value that indicates whether the field is private.
IsPublic Gets a value that indicates whether the field is public.
IsSpecialName Gets a value that indicates whether the field has a name that has special significance.
IsStatic Gets a value that indicates whether the field is static (Shared in Visual Basic).
MemberType Gets a value that indicates that this member is a field. (Overrides MemberInfo..::.MemberType.)
MetadataToken Gets a value that identifies a metadata element. (Inherited from MemberInfo.)
Module Gets the module in which the type that declares the member represented by the current MemberInfo is defined. (Inherited from MemberInfo.)
Name Gets the name of the current member. (Inherited from MemberInfo.)
ReflectedType Gets the class object that was used to obtain this instance of MemberInfo. (Inherited from MemberInfo.)

Top

Methods

Name Description
Equals Returns a value that indicates whether this instance is equal to a specified object. (Overrides MemberInfo..::.Equals(Object).)
Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetCustomAttributes(Boolean) When overridden in a derived class, returns an array of all custom attributes applied to this member. (Inherited from MemberInfo.)
GetCustomAttributes(Type, Boolean) When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type. (Inherited from MemberInfo.)
GetFieldFromHandle(RuntimeFieldHandle) Gets a FieldInfo for the field represented by the specified handle.
GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) Gets a FieldInfo for the field represented by the specified handle, for the specified generic type.
GetHashCode Returns the hash code for this instance. (Overrides MemberInfo..::.GetHashCode()()().)
GetRawConstantValue Returns a literal value associated with the field by a compiler.
GetType Gets the Type of the current instance. (Inherited from Object.)
GetValue When overridden in a derived class, returns the value of a field supported by a given object.
IsDefined When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member. (Inherited from MemberInfo.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
SetValue(Object, Object) Sets the value of the field that is supported by the given object.
SetValue(Object, Object, BindingFlags, Binder, CultureInfo) When overridden in a derived class, sets the value of the field with the specified constraints on type conversion.
ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Extension Methods

Name Description
GetCustomAttribute(Type) Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member. (Defined by CustomAttributeExtensions.)
GetCustomAttribute(Type, Boolean) Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member. (Defined by CustomAttributeExtensions.)
GetCustomAttribute<(Of <(T>)>)()()() Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member. (Defined by CustomAttributeExtensions.)
GetCustomAttribute<(Of <(T>)>)(Boolean) Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member. (Defined by CustomAttributeExtensions.)
GetCustomAttributes()()() Overloaded. Retrieves a collection of custom attributes that are applied to a specified member. (Defined by CustomAttributeExtensions.)
GetCustomAttributes(Type) Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member. (Defined by CustomAttributeExtensions.)
GetCustomAttributes<(Of <(T>)>)()()() Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member. (Defined by CustomAttributeExtensions.)
GetCustomAttributes<(Of <(T>)>)(Boolean) Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member. (Defined by CustomAttributeExtensions.)
IsDefined Indicates whether custom attributes of a specified type are applied to a specified member. (Defined by CustomAttributeExtensions.)

Top

Remarks

The FieldInfo class does not have a public constructor. FieldInfo objects are obtained by calling either Type..::.GetFields or Type..::.GetField.

Fields are variables that are defined in the class. FieldInfo provides access to the metadata for a field in a class and also enables late-bound access to the value of a field. In Windows Phone, you can get the metadata of any field, regardless of its access level, but you cannot use FieldInfo to bypass access-level protection when you get or set the value of a field.

Examples

The following example uses the Type..::.GetFields method to get the fields of the Example class, and then displays information about the fields.

Imports System.Reflection

Public Class Example
   Public myField1 As Integer = 0
   Protected myField2 As String = Nothing

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myFieldInfo() As FieldInfo
      Dim myType As Type = GetType(Example)
      ' Get the type and fields of Example.
      myFieldInfo = myType.GetFields(BindingFlags.NonPublic Or _
                    BindingFlags.Instance Or BindingFlags.Public)
      outputBlock.Text &= ControlChars.NewLine & "The fields of " & _
                    "Example class are " & ControlChars.NewLine & vbCrLf
      ' Display the field information of Example.
      Dim i As Integer
      For i = 0 To myFieldInfo.Length - 1
         outputBlock.Text &= String.Format(ControlChars.NewLine + "Name            : {0}", myFieldInfo(i).Name) & vbCrLf
         outputBlock.Text &= String.Format("Declaring Type  : {0}", myFieldInfo(i).DeclaringType) & vbCrLf
         outputBlock.Text &= String.Format("IsPublic        : {0}", myFieldInfo(i).IsPublic) & vbCrLf
         outputBlock.Text &= String.Format("MemberType      : {0}", myFieldInfo(i).MemberType) & vbCrLf
         outputBlock.Text &= String.Format("FieldType       : {0}", myFieldInfo(i).FieldType) & vbCrLf
         outputBlock.Text &= String.Format("IsFamily        : {0}", myFieldInfo(i).IsFamily) & vbCrLf
      Next i
   End Sub
End Class
using System;
using System.Reflection;

public class Example
{
   public int myField1 = 0;
   protected string myField2 = null;
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      FieldInfo[] myFieldInfo;
      Type myType = typeof(Example);
      // Get the type and fields of Example.
      myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
          | BindingFlags.Public);
      outputBlock.Text += "\nThe fields of " +
          "Example are \n" + "\n";
      // Display the field information of Example.
      for (int i = 0; i < myFieldInfo.Length; i++)
      {
         outputBlock.Text += String.Format("\nName            : {0}", myFieldInfo[i].Name) + "\n";
         outputBlock.Text += String.Format("Declaring Type  : {0}", myFieldInfo[i].DeclaringType) + "\n";
         outputBlock.Text += String.Format("IsPublic        : {0}", myFieldInfo[i].IsPublic) + "\n";
         outputBlock.Text += String.Format("MemberType      : {0}", myFieldInfo[i].MemberType) + "\n";
         outputBlock.Text += String.Format("FieldType       : {0}", myFieldInfo[i].FieldType) + "\n";
         outputBlock.Text += String.Format("IsFamily        : {0}", myFieldInfo[i].IsFamily) + "\n";
      }
   }
}

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

Thread Safety

This type is thread safe.

See Also

Reference

System.Reflection Namespace

Type

Type..::.GetFields

Type..::.GetField