C# | Type.GetField() Method - GeeksforGeeks

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
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:
// C# program to demonstrate the// Type.GetField(String) Methodusing 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 Studentpublic class Student {    public string Name = "Rahul";    public string Dept = "Electrical";    public int Roll = 10;}
FieldInfo is: System.String Name

Example 2: For ArgumentNullException
// C# program to demonstrate the// Type.GetField(String) Methodusing 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 Studentpublic class Student {    public string Name = "Rahul";    public string Dept = "Electrical";    public int Roll = 10;}
name is null.
Exception Thrown: System.ArgumentNullException

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:
// C# program to demonstrate the// Type.GetField(String) Methodusing 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 Studentpublic class Student {    public string Name = "Rahul";    public string Dept = "Electrical";    public int Roll = 10;}
FieldInfo is: System.String Name

Example 2: For ArgumentNullException
// C# program to demonstrate the// Type.GetField(String) Methodusing 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 Studentpublic class Student {    public string Name = "Rahul";    public string Dept = "Electrical";    public int Roll = 10;}
Output:
name is null.
Exception Thrown: System.ArgumentNullException

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.type.getfield?view=netframework-4.8
shubham_singh
CSharp-method
CSharp-Type-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Destructors in C#
Difference between Ref and Out keywords in C#
C# | Delegates
Extension Method in C#
C# | Constructors
Introduction to .NET Framework
C# | Abstract Classes
C# | Class and Object
C# | Data Types
C# | Encapsulation