C# | Type.GetProperties() Method

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

  • GetProperties() Method
  • GetProperties(BindingFlags) Method

GetProperties() Method

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



Syntax: public System.Reflection.PropertyInfo[] GetProperties ();

Return Value: This method returns an array of PropertyInfo objects representing all public properties of the current Type or an empty array of type PropertyInfo if the current Type does not have public properties.

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetProperties() Method
using System;
using System.Globalization;
using System.Reflection;
  
// Defining class Empty
public class Empty { }
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(string);
  
        // try-catch block for handling Exception
        try {
  
            // using GetProperties() Method
            PropertyInfo[] type = objType.GetProperties();
  
            // Display the Result
            Console.WriteLine("Properties of current type is: ");
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine(" {0}", type[i]);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Properties of current type is: 
 Int32 Length
 Char Chars [Int32]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetProperties() Method
using System;
using System.Globalization;
using System.Reflection;
  
// Defining class Empty
public class Empty {}
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(System.Type);
  
        // try-catch block for handling Exception
        try {
  
            // using GetProperties() Method
            PropertyInfo[] type = objType.GetProperties();
  
            // Display the Result
            Console.WriteLine("Properties of current type is: ");
            for (int i = 0; i < 10; i++)
                Console.WriteLine(" {0}", type[i]);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Properties of current type is: 
 System.Reflection.MemberTypes MemberType
 System.Type DeclaringType
 System.Reflection.MethodBase DeclaringMethod
 System.Type ReflectedType
 System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute
 System.Guid GUID
 System.Reflection.Binder DefaultBinder
 System.Reflection.Module Module
 System.Reflection.Assembly Assembly
 System.RuntimeTypeHandle TypeHandle

GetProperties(BindingFlags) Method

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

Syntax: public abstract System.Reflection.PropertyInfo[] GetProperties (System.Reflection.BindingFlags bindingAttr);
Here, it takes 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 PropertyInfo objects representing all properties of the current Type that match the specified binding constraints or an empty array of type PropertyInfo, if the current Type does not have properties, or if none of the properties match the binding constraints.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetProperties(BindingFlags)
// 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(Dog);
  
        // using GetProperties() Method
        PropertyInfo[] type = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
  
        // Display the Result
        Console.WriteLine("Properties of current type is: ");
        for (int i = 0; i < type.Length; i++)
            Console.WriteLine(" {0}", type[i]);
    }
}
  
// Defining class Dog
public class Dog {
  
    private string color, breed, type;
  
    // Constructor
    public Dog(string color, string breed, string type)
    {
        this.color = color;
        this.breed = breed;
        this.type = type;
    }
  
    // Color Property
    public String Color
    {
        get { return color; }
    }
  
    // Breed Property
    public String Breed
    {
        get { return breed; }
    }
  
    // Type Property
    public String Type
    {
        get { return type; }
    }
  
    // BloodGroup Property
    private String BloodGroup
    {
        get { return "AB+"; }
    }
}

chevron_right


Output:

Properties of current type is: 
 System.String Color
 System.String Breed
 System.String Type

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetProperties(BindingFlags)
// 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(Dog);
  
        // using GetProperties() Method
        PropertyInfo[] type = objType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
  
        // Display the Result
        Console.WriteLine("Properties of current type is: ");
        for (int i = 0; i < type.Length; i++)
            Console.WriteLine(" {0}", type[i]);
    }
}
  
// Defining class Dog
public class Dog {
  
    private string color, breed, type, bp;
  
    // Constructor
    public Dog(string color, string breed,
                   string type, string bp)
    {
        this.color = color;
        this.breed = breed;
        this.type = type;
        this.bp = bp;
    }
  
    // Color Property
    public String Color
    {
        get { return color; }
    }
  
    // Breed Property
    public String Breed
    {
        get { return breed; }
    }
  
    // Type Property
    public String Type
    {
        get { return type; }
    }
  
    // BloodGroup Property
    protected String BloodGroup
    {
        get { return "AB+"; }
    }
  
    // BloodPressure Property
    protected String BloodPressure
    {
        get { return bp; }
    }
}

chevron_right


Output:

Properties of current type is: 
 System.String BloodGroup
 System.String BloodPressure

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.