Export (0) Print
Expand All

FieldInfo Class

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

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public abstract class FieldInfo : MemberInfo, _FieldInfo
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public abstract class FieldInfo extends MemberInfo implements _FieldInfo
SerializableAttribute 
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public abstract class FieldInfo extends MemberInfo implements _FieldInfo
Not applicable.

The field information is obtained from metadata. The FieldInfo class 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.

The following example uses the Type.GetFields method to get the field-related information from the FieldInfo class, and then displays field attributes.

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);
        }
    }
}

import System.*;
import System.Reflection.*;

public class FieldInfoClass
{
    public int myField1 = 0;
    protected String myField2 = null;

    public static void main(String[] args)
    {
        FieldInfo myFieldInfo[];
        Type myType = FieldInfoClass.class.ToType();

        // 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].get_Name());
            Console.WriteLine("Declaring Type  : {0}", 
                myFieldInfo[i].get_DeclaringType());
            Console.WriteLine("IsPublic        : {0}", 
                System.Convert.ToString(myFieldInfo[i].get_IsPublic()));
            Console.WriteLine("MemberType      : {0}", 
                myFieldInfo[i].get_MemberType());
            Console.WriteLine("FieldType       : {0}",
                myFieldInfo[i].get_FieldType());
            Console.WriteLine("IsFamily        : {0}",
                System.Convert.ToString(myFieldInfo[i].get_IsFamily()));
        }
    } //main
} //FieldInfoClass

This type is thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft