Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FieldInfo Class

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

For a list of all members of this type, see FieldInfo Members.


[Visual Basic]
MustInherit Public Class FieldInfo
   Inherits MemberInfo
public abstract class FieldInfo : MemberInfo
public __gc __abstract class FieldInfo : public MemberInfo
abstract class FieldInfo extends MemberInfo

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


The field information is obtained from metadata. FieldInfo does not have a public constructor. FieldInfo objects are obtained by calling either the GetFields or GetField method of a Type object.

Fields are variables defined in the class. FieldInfo provides access to the metadata for a field within a class and provides dynamic set and get functionality for the field. The class is not loaded into memory until invoke or get is called on the object.

Notes to Inheritors:  When you inherit from FieldInfo, you must override the following members: GetValue and SetValue.


[Visual Basic, C#, C++] The following example uses Type.GetFields to get the field-related information from FieldInfoClass, and then displays field attributes.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

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

    Public Shared Sub Main()
        Dim myFieldInfo() As FieldInfo
        Dim myType As Type = GetType(FieldInfoClass)
        ' Get the type and fields of FieldInfoClass.
        myFieldInfo = myType.GetFields(BindingFlags.NonPublic Or _
                      BindingFlags.Instance Or BindingFlags.Public)
        Console.WriteLine(ControlChars.NewLine & "The fields of " & _
                      "FieldInfoClass class are " & ControlChars.NewLine)
        ' Display the field information of FieldInfoClass.
        Dim i As Integer
        For i = 0 To myFieldInfo.Length - 1
            Console.WriteLine(ControlChars.NewLine + "Name            : {0}", myFieldInfo(i).Name)
            Console.WriteLine("Declaring Type  : {0}", myFieldInfo(i).DeclaringType)
            Console.WriteLine("IsPublic        : {0}", myFieldInfo(i).IsPublic)
            Console.WriteLine("MemberType      : {0}", myFieldInfo(i).MemberType)
            Console.WriteLine("FieldType       : {0}", myFieldInfo(i).FieldType)
            Console.WriteLine("IsFamily        : {0}", myFieldInfo(i).IsFamily)
        Next i
    End Sub
End Class

using System;
using System.Reflection;

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

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

public __gc class FieldInfoClass
   int myField1;
   String* myField2;

int main() 
   FieldInfo* myFieldInfo[];
   Type* myType = __typeof(FieldInfoClass);
   // Get the type and fields of FieldInfoClass.
   myFieldInfo = myType->GetFields(static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance
      | BindingFlags::Public));
   Console::WriteLine(S"\nThe fields of FieldInfoClass are \n");
   // Display the field information of FieldInfoClass.
   for (int i = 0; i < myFieldInfo->Length; i++) {
      Console::WriteLine(S"\nName            : {0}", myFieldInfo[i]->Name);
      Console::WriteLine(S"Declaring Type  : {0}", myFieldInfo[i]->DeclaringType);
      Console::WriteLine(S"IsPublic        : {0}", __box(myFieldInfo[i]->IsPublic));
      Console::WriteLine(S"MemberType      : {0}", __box(myFieldInfo[i]->MemberType));
      Console::WriteLine(S"FieldType       : {0}", myFieldInfo[i]->FieldType);
      Console::WriteLine(S"IsFamily        : {0}", __box(myFieldInfo[i]->IsFamily));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Reflection

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

FieldInfo Members | System.Reflection Namespace | Type

© 2017 Microsoft