Skip to content
Related Articles

Related Articles

Improve Article
C# | Type.GetFields() Method
  • Last Updated : 05 Dec, 2019

Type.GetFields() Method is used to get the fields of the current Type. There are 2 methods in the overload list of this method as follows:

  • GetFields() Method
  • GetFields(BindingFlags) Method

GetFields() Method

This method is used to return all the public fields of the current Type.

Syntax: public System.Reflection.FieldInfo[] GetFields ();

Return Value: This method returns an array of FieldInfo objects representing all the public fields defined for the current Type. Or, an empty array of type FieldInfo, if no public fields are defined for the current Type.



Below programs illustrate the use of Type.GetFields() Method:

Example 1:




// C# program to demonstrate the
// Type.GetFields() 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 array of Fields by
            // using GetField() Method
            FieldInfo[] info = objType.GetFields(BindingFlags.Public | BindingFlags.Static);
  
            // Display the Result
            Console.Write("Fields of current type is as Follow: ");
            for (int i = 0; i < info.Length; i++)
                Console.WriteLine(" {0}", info[i]);
        }
  
        // 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;
    public static int id = 02;
}
Output:
Fields of current type is as Follow:  System.Int32 id

Example 2: For if no public field is defined




// C# program to demonstrate the
// Type.GetFields(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 array of Fields by
            // using GetField() Method
            FieldInfo[] info = objType.GetFields();
  
            // Display the Result
            Console.Write("Public Fields of current type is as follow: ");
            if (info.Length != 0) 
            {
                for (int i = 0; i < info.Length; i++)
                    Console.WriteLine(" {0}", info[i]);
            }
            else
                Console.WriteLine("No public fields are defined for the current Type.");
        }
  
        // 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
{ }
Output:
Public Fields of current type is as follow: No public fields are defined for the current Type.

GetFields(BindingFlags) Method

This method is used to search for the fields defined for the current Type, using the specified binding when overridden in a derived class, constraints.

Syntax: public abstract System.Reflection.FieldInfo[] GetFields (System.Reflection.BindingFlags bindingAttr);
Here, bindingAttr 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 array of FieldInfo objects representing all fields defined for the current Type that match the specified binding constraints. Or, an empty array of type FieldInfo, if no fields are defined for the current Type, or if none of the defined fields match the binding constraints.

Below programs illustrate the use of the above-discussed method:

Example 1:




// 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);
     
        // Creating 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




// C# program to demonstrate the 
// Type.GetFields(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(Book);
     
        // Creating try-catch block for handling Exception 
        try
     
            // Getting array of Fields by  
            // using GetField() Method 
            FieldInfo[] info = objType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); 
     
            // Display the Result
            Console.WriteLine("Fields of current type is as follow :-");
            for(int i=0; i<info.Length; i++)
            Console.WriteLine(" {0}",info[i]); 
        
     
        // 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 Book
{
    public string Name = "Element of Physics";
    public string Author = "R.S. Agrwal";
    public static int Price = 500;
    private string metadata = "djdieeiie";
   
}
Output:
FieldInfo is - System.String Name

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :