C# | Type.GetField() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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;
}

chevron_right


Output:

FieldInfo is: System.String Name

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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;
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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;
}

chevron_right


Output:

FieldInfo is: System.String Name

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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;
}

chevron_right


Output:

name is null.
Exception Thrown: System.ArgumentNullException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.