
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to fetch a property value dynamically in C#?
We can make use of Reflection to fetch a property value dynamically.
Reflection provides objects (of type Type) that describe assemblies, modules and types. We can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If we use attributes in our code, reflection enables us to access them.
The System.Reflection namespace and System.Type class play an important role in .NET Reflection. These two works together and allows us to reflect over many other aspects of a type.
Example
using System; using System.Text; 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); Console.ReadLine(); } 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; } } }
Output
The output of the above code is
Value of Property EmployeeId: 1 Value of Property EmployeeName: Mark
In the above example we could see that the Employee properties values are set using Reflection by getting the type and property name. Similarly for fetching the property value we have used GetProperty() method of the Reflection class. By using this we can fetch the value of any property during the runtime.
- Related Articles
- How to create and store property file dynamically in Java?
- How to set a property value by reflection in C#?
- How to dynamically allocate a 2D array in C?
- How to create arrays dynamically in C#?
- How to fetch the value from a KeyValue Tuple in Java?
- Query an array in MongoDB to fetch a specific value
- How to dynamically update a listView in Android?
- How to add a button dynamically in android?
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- Fetch a specific column value (name) in MySQL
- Fetch a value between different values in MySQL
- How do you give a C# Auto-Property a default value?
- How to set a property having different datatype with a string value using reflection in C#?
- How to dynamically load a Python class?
- How to load a JavaScript file Dynamically?
