FieldInfo Class

System.Reflection

public abstract class FieldInfo : MemberInfo

Represents the metadata for a field and provides access to field members such as name, attributes, and type. Use the members of this class to get information about the field.

Fields are members of a type that represent data that is stored within instances of the type, or within the type itself (static fields).

Inheritance

Object
MemberInfo
FieldInfo

Remarks

The FieldInfo class provides methods to retrieve information about fields, such as their name, type, and modifiers. You can also use it to get and set the value of a field.

Fields

The FieldInfo class itself does not declare any public fields. However, it inherits members from MemberInfo.

Methods

Name Description
GetValue(Object obj) When overridden in a derived class, gets the value of the field on a particular object.
SetValue(Object obj, Object value) When overridden in a derived class, sets the value of the field on a particular object.
GetRawConstantValue() Gets the value of the field. This is used for fields that are not constants.
IsLiteral Gets a value that indicates whether the field is a literal.
IsInitOnly Gets a value that indicates whether the field is read-only.

Properties

Name Description
FieldType Gets the Type of the field.
IsPublic Gets a value indicating whether the field is public.
IsPrivate Gets a value indicating whether the field is private.
IsFamily Gets a value indicating whether the field is protected.
IsAssembly Gets a value indicating whether the field is internal.
IsStatic Gets a value indicating whether the field is static.

Example

// Get the Type object for a class
Type myType = typeof(MyClass);

// Get all public fields of the class
FieldInfo[] fieldInfos = myType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

foreach (FieldInfo fieldInfo in fieldInfos)
{
    Console.WriteLine($"Field Name: {fieldInfo.Name}");
    Console.WriteLine($"Field Type: {fieldInfo.FieldType}");
    Console.WriteLine($"Is Static: {fieldInfo.IsStatic}");
    Console.WriteLine($"Is Literal: {fieldInfo.IsLiteral}");
    Console.WriteLine($"Is InitOnly: {fieldInfo.IsInitOnly}");
    Console.WriteLine("---");
}

// Example usage of GetValue and SetValue
MyClass myObject = new MyClass();
FieldInfo instanceField = myType.GetField("myInstanceField");
if (instanceField != null)
{
    instanceField.SetValue(myObject, 123);
    Console.WriteLine($"New value of myInstanceField: {instanceField.GetValue(myObject)}");
}

FieldInfo staticField = myType.GetField("myStaticField");
if (staticField != null)
{
    staticField.SetValue(null, "Hello Reflection");
    Console.WriteLine($"New value of myStaticField: {staticField.GetValue(null)}");
}

// Define a sample class for demonstration
public class MyClass
{
    public int myInstanceField;
    public static string myStaticField;
    public const string MyConstant = "This is a constant";
    public readonly int ReadOnlyField = 42;
}
Keywords: Reflection, Field, Metadata, Type, MemberInfo