In this tutorial, we will explore the Type.GetField method in C# along with its syntax and illustration cases.
What is the Type.GetField Method?
The Type.GetField method retrieves a particular public field from a type by taking the field name as an argument. When the specified field is found, it generates a FieldInfo object that represents that field. If the field is not found, it returns null. This approach allows for the dynamic retrieval of fields based on their names at runtime, facilitating tasks like reading or modifying field values and accessing metadata like the field's type or attributes through reflection. This functionality proves beneficial in scenarios requiring dynamic field access determination or when inspecting and altering types and their elements during program execution.
Type.GetFields method is employed to fetch the fields associated with the current Type. Within its overload list, you can find the two methods listed below:
-
- GetFields Method
-
- GetFields(BindingFlags) Method
Syntax:
It has the following syntax:
public System.Reflection.FieldInfo[] GetFields ();
The return value of this function is an array containing FieldInfo objects that represent all the public fields defined within the current Type. When there are no public fields defined for the current Type, the function will return an empty array of type FieldInfo.
Example 1:
Let's consider a program to demonstrate the implementation of the Type.GetFields method in the C# programming language.
using System;
using System.Globalization;
using System.Reflection;
class GetFields{
// Main Method
public static void Main()
{
// object declarations
Type objTyp = typeof(Employee);
//Try block for exception handling
try {
FieldInfo[] information = objTyp.GetFields(BindingFlags.Public | BindingFlags.Static);
// result display
Console.Write("The Fields of the current type are as Follows: ");
for (int i = 0; i < information.Length; i++)
Console.WriteLine(" {0}", information[i]);
}
// Argument exception
catch (ArgumentNullException a)
{
Console.Write("The name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", a.GetType(), a.Message);
}
}
}
// Defining class Employee
public class Employee
{
public string EmployeeName = "Rohan";
public string EmployeeNameDept = "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:
Let's consider another instance to demonstrate the implementation of the Type.GetFields method in the C# programming language.
using System;
using System.Globalization;
using System.Reflection;
class GetField2{
// Main Method
public static void Main()
{
// variable declaration
Type objTyp = typeof(Employee);
// exception handling
try {
// the getField method
FieldInfo[] information = objTyp.GetFields();
// print the result
Console.Write("The Public Fields of the current type is as follow: ");
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.");
}
// the catch argument
catch (ArgumentNullException a)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", a.GetType(), a.Message);
}
}
}
// the class student
public class Employee
{
}
Output:
The Public Fields of the current type is as follow: There were no public fields.
GetFields(BindingFlags) method
The GetFields(BindingFlags) method is employed to retrieve the module's global field that aligns with the specified binding flags.
Syntax:
It has the following syntax:
public abstract System.Reflection.FieldInfo[] GetFields
(System.Reflection.BindingFlags bindingAttr);
Here, bindingAttr represents a bitmask containing one or more BindingFlags that specify the search method or Zero to return null.
Return Value:
This function returns an array of FieldInfo objects that encompass all fields declared within the current Type and meet the specified binding conditions. In cases where no fields are declared for the current Type or none of the specified fields meet the binding criteria, an empty array of type FieldInfo is returned.
Example:
Let's consider an illustration to explain the Type.GetFields(Binding) method in C#.
using System;
using System.Globalization;
using System.Reflection;
class GetfeildsBinding {
// Main Method
public static void Main()
{
// an object declaration
Type objType = typeof(Employee);
// A try-catch block for handling exceptions
try {
BindingFlags bat = BindingFlags.Public | BindingFlags.Instance;
// the field info
FieldInfo information = objType.GetField("Name", bat);
// Display of the result
Console.WriteLine("The FieldInfo is - {0}",information);
}
// catch block
catch (ArgumentNullException ax)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", ax.GetType(), ax.Message);
}
}
}
// Defining class Employee
public class Employee
{
public string EmployeeName = "Rohan";
public string EmployeeDeptName = "Computer Science";
public int EmployeeIdNum = 15;
}
Output:
The FieldInfo is - System.String Name