Type.GetProperties() Method in C#

The Type.GetProperties() method in C# is used to retrieve information about the properties of a specific type at runtime using reflection. This method returns an array of PropertyInfo objects that represent all the properties of the specified type.

Syntax

Following are the two main overloads of the GetProperties() method −

public PropertyInfo[] GetProperties();
public PropertyInfo[] GetProperties(BindingFlags bindingAttr);

Parameters

  • bindingAttr − A bitwise combination of BindingFlags enumeration values that specify how the search is conducted. This parameter controls which properties are returned based on their accessibility and binding characteristics.

Return Value

Returns an array of PropertyInfo objects representing all the properties of the current type that match the specified binding constraints, or an empty array if no properties are found.

Using GetProperties() with Default Binding

The parameterless overload returns all public properties of the type −

using System;
using System.Reflection;

public class Demo {
    public static void Main() {
        Type type = typeof(string);
        PropertyInfo[] info = type.GetProperties();
        Console.WriteLine("Count of Properties = " + info.Length);
        Console.WriteLine("Properties...");
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(" {0}", info[i].ToString());
    }
}

The output of the above code is −

Count of Properties = 2
Properties...
Char Chars [Int32]
Int32 Length

Using GetProperties() with Custom Class

Here's an example demonstrating GetProperties() with a custom class that has different types of properties −

using System;
using System.Reflection;

public class Student {
    public string Name { get; set; }
    public int Age { get; set; }
    private string Id { get; set; }
    public static string School { get; set; } = "Default School";
}

public class Demo {
    public static void Main() {
        Type type = typeof(Student);
        PropertyInfo[] publicProperties = type.GetProperties();
        Console.WriteLine("Public Properties:");
        foreach (PropertyInfo prop in publicProperties) {
            Console.WriteLine(" - {0} ({1})", prop.Name, prop.PropertyType.Name);
        }
        
        PropertyInfo[] allProperties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
        Console.WriteLine("\nAll Properties (including private and static):");
        foreach (PropertyInfo prop in allProperties) {
            Console.WriteLine(" - {0} ({1})", prop.Name, prop.PropertyType.Name);
        }
    }
}

The output of the above code is −

Public Properties:
 - Name (String)
 - Age (Int32)
 - School (String)

All Properties (including private and static):
 - Name (String)
 - Age (Int32)
 - Id (String)
 - School (String)

Using BindingFlags for Specific Property Selection

The BindingFlags parameter allows you to filter properties based on specific criteria −

using System;
using System.Reflection;

public class Employee {
    public string Name { get; set; }
    private int salary;
    public int Salary { 
        get { return salary; } 
        set { salary = value; } 
    }
    public static string Company { get; set; } = "TechCorp";
}

public class Demo {
    public static void Main() {
        Type type = typeof(Employee);
        
        // Get only instance properties
        PropertyInfo[] instanceProps = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine("Instance Properties:");
        foreach (PropertyInfo prop in instanceProps) {
            Console.WriteLine(" - {0}", prop.Name);
        }
        
        // Get only static properties
        PropertyInfo[] staticProps = type.GetProperties(BindingFlags.Public | BindingFlags.Static);
        Console.WriteLine("\nStatic Properties:");
        foreach (PropertyInfo prop in staticProps) {
            Console.WriteLine(" - {0}", prop.Name);
        }
    }
}

The output of the above code is −

Instance Properties:
 - Name
 - Salary

Static Properties:
 - Company

Common BindingFlags Values

BindingFlags Description
Public Include public properties
NonPublic Include non-public properties
Instance Include instance properties
Static Include static properties
DeclaredOnly Include only properties declared in the type itself

Conclusion

The Type.GetProperties() method is a powerful reflection tool that allows you to inspect properties of any type at runtime. Using BindingFlags, you can precisely control which properties are retrieved, making it useful for scenarios like serialization, object mapping, and dynamic property access.

Updated on: 2026-03-17T07:04:35+05:30

556 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements