System.Reflection.FieldInfo

Represents a field of a type and provides access to the field's metadata.

Summary

The FieldInfo class in the System.Reflection namespace is used to get information about fields of a type. Fields are members of a type that represent data storage. Reflection allows you to inspect and manipulate types, methods, and members at runtime.

This class provides access to properties such as the field's name, type, attributes, and its value for a specific instance.

Inheritance

Fields

The FieldInfo class itself does not expose public fields directly. Its functionality is accessed through its properties and methods.

Properties

Public Properties

  • 🔸
    FieldHandle: RuntimeFieldHandle

    Gets a RuntimeFieldHandle, which is a handle to the internal metadata representation of the field.

  • 🔸
    FieldType: Type

    Gets the Type that represents the field.

  • 🔸
    IsAssembly: bool

    Gets a value that indicates whether the field is accessible within the assembly.

  • 🔸
    IsContextful: bool

    Gets a value that indicates whether the field is context-bound.

  • 🔸
    IsFamily: bool

    Gets a value that indicates whether the field is accessible only by derived classes.

  • 🔸
    IsFamilyAndAssembly: bool

    Gets a value that indicates whether the field is accessible by derived classes and within the same assembly.

  • 🔸
    IsFamilyOrAssembly: bool

    Gets a value that indicates whether the field is accessible by derived classes, or by any class in the same assembly.

  • 🔸
    IsInitOnly: bool

    Gets a value that indicates whether the field can be set only during construction of the containing class or property.

  • 🔸
    IsLiteral: bool

    Gets a value that indicates whether the field is a literal (static readonly).

  • 🔸
    IsNotSerialized: bool

    Gets a value that indicates whether the field is not marked with the [NonSerialized] attribute.

  • 🔸
    IsPInvokeImpl: bool

    Gets a value that indicates whether the field is defined in an unmanaged code.

  • 🔸
    IsPrivate: bool

    Gets a value that indicates whether the field is private.

  • 🔸
    IsPublic: bool

    Gets a value that indicates whether the field is public.

  • 🔸
    IsSecurityCritical: bool

    Gets a value that indicates whether the field is critical, that is, it can be called by transparent code and the CLR security system.

  • 🔸
    IsSecurityTransparent: bool

    Gets a value that indicates whether the field is transparent, that is, it can be called by any code.

  • 🔸
    IsSecurityRuleSet: bool

    Gets a value that indicates whether the field is at the highest level of the security transparency model.

  • 🔸
    IsSpecialName: bool

    Gets a value that indicates whether this member is a special name.

  • 🔸
    IsStatic: bool

    Gets a value that indicates whether the field is static.

  • 🔸
    MemberType: MemberTypes

    Gets the type of the specified member.

  • 🔸
    Name: string

    Gets the name of the current member.

  • 🔸
    ReflectedType: Type

    Gets the class that is used to coarse-grained reflection on the member.

  • 🔸
    StreamKind: FieldAttributes

    Gets the custom attributes of the field.

Methods

Public Methods

  • ⚙️
    GetValue(object obj): object

    Returns the value of the specified field on the specified object.

  • ⚙️
    SetValue(object obj, object value)

    Sets the value of the specified field on the specified object to the specified new value.

  • ⚙️
    GetCustomAttributes(bool inherit): object[]

    When overridden in a derived class, searches the current symbol store and the grab-bag of symbols for custom attributes and returns them all.

  • ⚙️
    GetCustomAttributes(Type attributeType, bool inherit): object[]

    When overridden in a derived class, searches for the custom attributes of the specified type and returns them.

  • ⚙️
    IsDefined(Type attributeType, bool inherit): bool

    When overridden in a derived class, indicates whether the specified attribute is present.

  • ⚙️
    Equals(object obj): bool

    Determines whether the specified object instances are equal.

  • ⚙️
    GetHashCode(): int

    Returns the hash code for this instance.

  • ⚙️
    ToString(): string

    Returns the fully qualified name of this member.

Example


using System;
using System.Reflection;

public class MyClass
{
    public int publicField = 10;
    private string privateField = "Hello";
    public static double staticField = 3.14;
}

public class ReflectionExample
{
    public static void Main(string[] args)
    {
        Type myType = typeof(MyClass);

        // Get all public fields
        FieldInfo[] publicFields = myType.GetFields(BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine("Public Instance Fields:");
        foreach (FieldInfo field in publicFields)
        {
            Console.WriteLine($"- {field.Name} ({field.FieldType})");

            // Get value of publicField for an instance
            MyClass instance = new MyClass();
            object value = field.GetValue(instance);
            Console.WriteLine($"  Value: {value}");
        }

        // Get all private fields
        FieldInfo[] privateFields = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
        Console.WriteLine("\nPrivate Instance Fields:");
        foreach (FieldInfo field in privateFields)
        {
            Console.WriteLine($"- {field.Name} ({field.FieldType})");

            // Get value of privateField for an instance
            MyClass instance = new MyClass();
            object value = field.GetValue(instance);
            Console.WriteLine($"  Value: {value}");
        }

        // Get static fields
        FieldInfo[] staticFields = myType.GetFields(BindingFlags.Public | BindingFlags.Static);
        Console.WriteLine("\nPublic Static Fields:");
        foreach (FieldInfo field in staticFields)
        {
            Console.WriteLine($"- {field.Name} ({field.FieldType})");

            // Get value of staticField
            object value = field.GetValue(null); // Pass null for static fields
            Console.WriteLine($"  Value: {value}");
        }

        // Setting a value
        Console.WriteLine("\nSetting a value:");
        FieldInfo fieldToSet = myType.GetField("publicField", BindingFlags.Public | BindingFlags.Instance);
        if (fieldToSet != null)
        {
            MyClass instanceToModify = new MyClass();
            Console.WriteLine($"Original value of publicField: {fieldToSet.GetValue(instanceToModify)}");
            fieldToSet.SetValue(instanceToModify, 99);
            Console.WriteLine($"New value of publicField: {fieldToSet.GetValue(instanceToModify)}");
        }
    }
}