C# | Type.GetField() Method
Last Updated :
10 Dec, 2019
Type.GetField() Method is used to get a specific field of the current Type. There are 2 methods in the overload list of this method as follows:
- GetField(String) Method
- GetField(String, BindingFlags) Method
GetField(String) Method
This method is used to search for the public field with the specified name.
Syntax: public System.Reflection.FieldInfo GetField (string name);
Here, it takes the string containing the name of the data field to get.
Return Value: This method returns an object representing the public field with the specified name if found otherwise, null.
Exception: This method throws ArgumentNullException if the name is null .
Below programs illustrate the use of
Type.GetField() Method:
Example 1:
csharp
// C# program to demonstrate the
// Type.GetField(String) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Student);
// try-catch block for handling Exception
try {
// Getting FieldInfo by
// using GetField(String) Method
FieldInfo info = objType.GetField("Name");
// Display the Result
Console.WriteLine("FieldInfo is: {0}", info);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
}
Output:
FieldInfo is: System.String Name
Example 2: For
ArgumentNullException
csharp
// C# program to demonstrate the
// Type.GetField(String) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Student);
// try-catch block for handling Exception
try {
// Getting FieldInfo by
// using GetField() Method
FieldInfo info = objType.GetField(null);
// Display the Result
Console.WriteLine("FieldInfo is: {0}", info);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("Name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
}
Output:
name is null.
Exception Thrown: System.ArgumentNullException
GetField(String, BindingFlags) Method
This method is used to search for the specified field, using the specified binding constraints.
Syntax: public abstract System.Reflection.FieldInfo GetField (string name, System.Reflection.BindingFlags bindingAttr);
Parameters:
name: The string containing the name of the data field to get.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted or Zero, to return null.
Return Value: This method returns an object representing the field that matches the specified requirements if found otherwise, null.
Exception: This method throws
ArgumentNullException if name is null.
Below programs illustrate the use of the above-discussed method:
Example 1:
csharp
// C# program to demonstrate the
// Type.GetField(String) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Student);
// try-catch block for handling Exception
try {
// You must specify either BindingFlags.Instance
// or BindingFlags.Static
// and Specify BindingFlags.Public
// to include public fields in the search
BindingFlags battr = BindingFlags.Public | BindingFlags.Instance;
// Getting FieldInfo by
// using GetField() Method
FieldInfo info = objType.GetField("Name", battr);
// Display the Result
Console.WriteLine("FieldInfo is: {0}", info);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
}
Output:
FieldInfo is: System.String Name
Example 2: For
ArgumentNullException
csharp
// C# program to demonstrate the
// Type.GetField(String) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Student);
// try-catch block for handling Exception
try {
// You must specify either BindingFlags.Instance
// or BindingFlags.Static
// and Specify BindingFlags.Public
// to include public fields in the search
BindingFlags battr = BindingFlags.Public | BindingFlags.Instance;
// Getting FieldInfo by
// using GetField() Method
FieldInfo info = objType.GetField(null, battr);
// Display the Result
Console.WriteLine("FieldInfo is: {0}", info);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
}
Output:
name is null.
Exception Thrown: System.ArgumentNullException
Reference: