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 discovered, it produces a FieldInfo object representing that field; otherwise, it returns null. This technique enables dynamic access to fields by their given names during runtime, enabling reflection-based activities such as reading or updating field values and retrieving metadata about the field, such as its type or attributes. It is particularly useful for situations where field access must be determined dynamically or when evaluating and changing types and their members at runtime.
Type.GetFields method is utilized to retrieve the fields of the current Type. This method's overload list contains the following two methods:
- GetFields Method
- GetFields(BindingFlags) Method
Syntax:
It has the following syntax:
public System.Reflection.FieldInfo[] GetFields ();
Return value: This function generates an array of FieldInfo objects expressing all of the public fields declared for the current Type. If there are no public fields declared for the current Type, an empty array of type FieldInfo is returned.
Example 1:
Let us take a program to illustrate the Type.GetFields method in C#.
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 us take another example to illustrate the Type.GetFields method in C#.
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 used to return the global field defined on the module, which matches the defined binding flags.
Syntax:
It has the following syntax:
public abstract System.Reflection.FieldInfo[] GetFields
(System.Reflection.BindingFlags bindingAttr);
Here, bindingAttr is a bitmask consisting of one or more BindingFlags that describe how the search is performed or Zero to return null.
Return Value:
This function provides an array of FieldInfo objects containing all fields declared for the current Type that fulfill the set of binding requirements. If no fields are declared for the current Type, or neither of the defined fields fulfill the binding criteria, a blank array of type FieldInfo is returned.
Example:
Let us take an example to illustrate 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