Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to fetch a property value dynamically in C#?
We can use Reflection to fetch a property value dynamically in C#. Reflection provides objects of type Type that describe assemblies, modules, and types, allowing us to dynamically access and manipulate object properties at runtime without knowing them at compile time.
The System.Reflection namespace and System.Type class work together to enable dynamic property access through methods like GetProperty() and GetValue().
Syntax
Following is the syntax for getting a property value dynamically −
Type type = typeof(ClassName);
PropertyInfo property = type.GetProperty("PropertyName");
object value = property.GetValue(instanceObject, null);
Following is the syntax for setting a property value dynamically −
Type type = typeof(ClassName);
PropertyInfo property = type.GetProperty("PropertyName");
property.SetValue(instanceObject, newValue);
Using Reflection for Dynamic Property Access
Example
using System;
using System.Reflection;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
var employeeType = typeof(Employee);
var employee = Activator.CreateInstance(employeeType);
SetPropertyValue(employeeType, "EmployeeId", employee, 1);
SetPropertyValue(employeeType, "EmployeeName", employee, "Mark");
GetPropertyValue(employeeType, "EmployeeId", employee);
GetPropertyValue(employeeType, "EmployeeName", employee);
}
static void SetPropertyValue(Type type, string propertyName, object instanceObject, object value) {
type.GetProperty(propertyName).SetValue(instanceObject, value);
}
static void GetPropertyValue(Type type, string propertyName, object instanceObject) {
Console.WriteLine($"Value of Property {propertyName}: {type.GetProperty(propertyName).GetValue(instanceObject, null)}");
}
}
public class Employee {
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
}
}
The output of the above code is −
Value of Property EmployeeId: 1 Value of Property EmployeeName: Mark
Using PropertyInfo Directly
You can also work with PropertyInfo objects directly for better performance when accessing the same property multiple times −
using System;
using System.Reflection;
class Student {
public string Name { get; set; }
public int Age { get; set; }
public string Course { get; set; }
}
class Program {
static void Main() {
Student student = new Student { Name = "Alice", Age = 20, Course = "Computer Science" };
Type studentType = student.GetType();
// Get all properties
PropertyInfo[] properties = studentType.GetProperties();
Console.WriteLine("Student Properties:");
foreach (PropertyInfo property in properties) {
object value = property.GetValue(student);
Console.WriteLine($"{property.Name}: {value}");
}
// Get specific property by name
PropertyInfo nameProperty = studentType.GetProperty("Name");
if (nameProperty != null) {
Console.WriteLine($"\nSpecific property - Name: {nameProperty.GetValue(student)}");
}
}
}
The output of the above code is −
Student Properties: Name: Alice Age: 20 Course: Computer Science Specific property - Name: Alice
Error Handling for Dynamic Property Access
When working with dynamic property access, it's important to handle cases where properties might not exist −
using System;
using System.Reflection;
class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Program {
static void Main() {
Person person = new Person { FirstName = "John", LastName = "Doe" };
// Try to get existing property
GetPropertySafely(person, "FirstName");
// Try to get non-existing property
GetPropertySafely(person, "MiddleName");
}
static void GetPropertySafely(object obj, string propertyName) {
Type type = obj.GetType();
PropertyInfo property = type.GetProperty(propertyName);
if (property != null) {
object value = property.GetValue(obj);
Console.WriteLine($"{propertyName}: {value ?? "null"}");
} else {
Console.WriteLine($"Property '{propertyName}' not found in {type.Name}");
}
}
}
The output of the above code is −
FirstName: John Property 'MiddleName' not found in Person
Conclusion
Reflection in C# enables dynamic property access using Type.GetProperty() and PropertyInfo.GetValue() methods. This powerful feature allows runtime property manipulation without compile-time knowledge, making it useful for scenarios like data binding, serialization, and configuration-driven applications.
