Information
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.

System.Object
   System.Reflection.MemberInfo
      System.Reflection.FieldInfo
         System.Reflection.Emit.FieldBuilder

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDual)>
MustInherit Public Class FieldInfo
   Inherits MemberInfo
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
public abstract class FieldInfo : MemberInfo
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDual)]
public __gc __abstract class FieldInfo : public MemberInfo
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDual)
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.

Remarks

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.

Example

[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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

public __gc class FieldInfoClass
{
public:
   int myField1;
protected:
   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.

Requirements

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft