C# Type.GetField() Method: Dynamically Accessing Public Fields

Learn how to use the Type.GetField() method in C# to dynamically access and manipulate public fields of a type at runtime. This guide covers its syntax, usage examples, and practical applications in reflection-based programming.



Type.GetField() Method in C#

In this article, we will discuss the Type.GetField() method in C# with its syntax and examples.

What is the Type.GetField() Method?

The Type.GetField() function returns a specified public field of a type. It accepts the field name as an input, and if the field is found, it returns a FieldInfo object representing that field. Otherwise, it returns null. This method allows dynamic access to fields by their given names at runtime, enabling reflection-based activities such as reading or updating field values and retrieving metadata like the field's type or attributes. It is useful when field access needs to be determined dynamically or when evaluating and changing types and their members at runtime.

Type.GetFields() Method

The Type.GetFields() method retrieves the fields of the current Type. The method has the following overloads:

  • GetFields() Method
  • GetFields(BindingFlags) Method

Syntax

The syntax for the GetFields() method is:


public System.Reflection.FieldInfo[] GetFields ();
        

Return Value

This method returns an array of FieldInfo objects representing all public fields declared for the current Type. If there are no public fields, it returns an empty array of FieldInfo objects.

Example 1

Let’s take a program to illustrate the Type.GetFields() method in C#.

Syntax

using System;   
using System.Globalization;   
using System.Reflection;   

class GetFields{   
    public static void Main() {   
        Type objTyp = typeof(Employee);   
        try {   
            FieldInfo[] information = objTyp.GetFields(BindingFlags.Public | BindingFlags.Static);   
            Console.Write("The Fields of the current type are as Follows: ");   
            for (int i = 0; i < information.Length; i++)   
                Console.WriteLine(" {0}", information[i]);   
        }   
        catch (ArgumentNullException a) {   
            Console.Write("The name is null.");   
            Console.Write("Exception Thrown: ");   
            Console.Write("{0}", a.GetType(), a.Message);   
        }   
    }   
}   

public class Employee {   
    public string EmployeeName = "Rohan";   
    public string EmployeeDept = "Computer Science";   
    public int EmployeeID = 15;   
    public static int idNum = 07;   
}
            

Output


The Fields of the current type are as Follows: 
System.Int32 idNum
            

Example 2

Here’s another example to illustrate the Type.GetFields() method in C#.

Syntax

using System;   
using System.Globalization;   
using System.Reflection;   

class GetField2{   
    public static void Main() {   
        Type objTyp = typeof(Employee);   
        try {   
            FieldInfo[] information = objTyp.GetFields();   
            Console.Write("The Public Fields of the current type are as follows: ");   
            if (information.Length != 0) {   
                for (int i = 0; i < information.Length; i++)   
                    Console.WriteLine(" {0}", information[i]);   
            } else {   
                Console.WriteLine("There were no public fields.");   
            }   
        } catch (ArgumentNullException a) {   
            Console.Write("Name is null.");   
            Console.Write("Exception Thrown: ");   
            Console.Write("{0}", a.GetType(), a.Message);   
        }   
    }   
}   

public class Employee {}
            

Output


The Public Fields of the current type are as follows:
There were no public fields.
            

GetFields(BindingFlags) Method

The GetFields(BindingFlags) method returns the global field defined in the module that matches the defined binding flags.

Syntax

The syntax for the GetFields(BindingFlags) method is:


public abstract System.Reflection.FieldInfo[] GetFields(System.Reflection.BindingFlags bindingAttr);
        

Return Value

This method returns an array of FieldInfo objects containing all fields declared for the current Type that fulfill the binding requirements. If no fields are declared or none match the binding criteria, it returns an empty array of FieldInfo objects.

Example

Let us take an example to illustrate the GetFields(BindingFlags) method in C#.

Syntax

using System;   
using System.Globalization;   
using System.Reflection;   

class GetFieldsBinding {   
    public static void Main() {   
        Type objType = typeof(Employee);   
        try {   
            BindingFlags bat = BindingFlags.Public | BindingFlags.Instance;   
            FieldInfo information = objType.GetField("EmployeeName", bat);   
            Console.WriteLine("The FieldInfo is - {0}", information);   
        } catch (ArgumentNullException ax) {   
            Console.Write("Name is null.");   
            Console.Write("Exception Thrown: ");   
            Console.Write("{0}", ax.GetType(), ax.Message);   
        }   
    }   
}   

public class Employee {   
    public string EmployeeName = "Rohan";   
    public string EmployeeDeptName = "Computer Science";   
    public int EmployeeIdNum = 15;   
}
            

Output


The FieldInfo is - System.String EmployeeName