How to Get Properties of a Class Using Reflection in C#
Use
Type.GetProperties() method to get all properties of a class in C#. First, get the Type object of the class using typeof(ClassName) or instance.GetType(), then call GetProperties() to retrieve an array of PropertyInfo objects representing each property.Syntax
To get properties of a class using reflection, follow these steps:
- Get the
Typeobject of the class usingtypeof(ClassName)orinstance.GetType(). - Call
GetProperties()on theTypeobject to get an array ofPropertyInfoobjects. - Use the
PropertyInfoobjects to access property names, types, and values.
csharp
Type type = typeof(ClassName); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine(property.Name); }
Example
This example shows how to get and print all property names and their values from an instance of a class using reflection.
csharp
using System; using System.Reflection; public class Person { public string Name { get; set; } public int Age { get; set; } private string Secret { get; set; } = "hidden"; } class Program { static void Main() { Person person = new Person { Name = "Alice", Age = 30 }; Type type = person.GetType(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { object value = property.GetValue(person); Console.WriteLine($"{property.Name}: {value}"); } } }
Output
Name: Alice
Age: 30
Common Pitfalls
Common mistakes when using reflection to get properties include:
- Not specifying binding flags and missing non-public or static properties.
- Trying to get values of properties without an instance (for instance properties).
- Assuming
GetProperties()returns properties in any guaranteed order.
Always specify BindingFlags if you want to include private or static properties, and use an instance when getting values of instance properties.
csharp
/* Wrong: Missing BindingFlags, so private properties are not included */ PropertyInfo[] props = typeof(Person).GetProperties(); /* Right: Include private and public instance properties */ PropertyInfo[] allProps = typeof(Person).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Quick Reference
Summary of key methods and flags for getting properties via reflection:
| Method/Flag | Description |
|---|---|
| typeof(ClassName) | Gets the Type object for a class. |
| instance.GetType() | Gets the Type object from an instance. |
| GetProperties() | Returns public instance properties by default. |
| GetProperties(BindingFlags) | Returns properties filtered by binding flags. |
| BindingFlags.Public | Includes public properties. |
| BindingFlags.NonPublic | Includes private, protected, and internal properties. |
| BindingFlags.Instance | Includes instance properties. |
| BindingFlags.Static | Includes static properties. |
Key Takeaways
Use Type.GetProperties() to retrieve properties of a class via reflection.
Specify BindingFlags to include non-public or static properties as needed.
Use an instance to get values of instance properties with PropertyInfo.GetValue().
Reflection does not guarantee property order; do not rely on it.
Private properties require BindingFlags.NonPublic to be accessed.