How to get a property value based on the name

How to Get a Property Value Based on the Name 💡
Have you ever found yourself in a situation where you needed to retrieve the value of a specific property from an object, but all you had was the property name? 🤔 Fear not! In this blog post, we'll explore an easy solution to this problem that will save you time and headaches. Let's dive right in! 🚀
The Problem at Hand 👀
Imagine you have a class called Car that inherits from a base class called Vehicle. Within the Car class, you have a property called Make, which represents the make of the car. Now, you want to write a method that takes the name of a property as input and returns its corresponding value. So, for instance, if you pass in "Make", the method should return "Ford", as it's the value of the Make property in the car object.
The Solution 🎉
To achieve this, we can make use of Reflection, a powerful feature in C# that allows us to analyze and manipulate types at runtime. Reflection provides us with methods and properties that enable us to examine metadata, properties, and fields of an object dynamically.
Here's how you can implement the GetPropertyValue method:
public string GetPropertyValue(object obj, string propertyName)
{
var propertyInfo = obj.GetType().GetProperty(propertyName);
return propertyInfo.GetValue(obj).ToString();
}Let's break down what's happening here:
We start by getting the type of the object using the
GetTypemethod. This gives us access to the metadata of the object, including its properties.Next, we use the
GetPropertymethod to retrieve thePropertyInfoobject associated with the givenpropertyName. ThisPropertyInfoobject contains valuable information about the property, such as its value and data type.Finally, we can use the
GetValuemethod on thePropertyInfoobject to fetch the value of the property from the actual object. We then convert it to a string usingToString()and return the result.
Putting It Into Action 🛠️
Now, let's apply our solution to the example you provided:
var car = new Car { Make = "Ford" };
var propertyName = "Make";
var propertyValue = GetPropertyValue(car, propertyName);
Console.WriteLine(propertyValue); // Output: "Ford"In this code snippet, we create a new Car object with the Make property set to "Ford". We then pass this car object along with the property name "Make" to the GetPropertyValue method, which returns the value of the Make property as expected.
A Word of Caution ⚠️
While Reflection can be powerful and handy, it does come with a performance cost. Reflection involves runtime evaluation and can impact performance in a noticeable way, especially if used extensively or in performance-critical scenarios. Therefore, it's important to use Reflection judiciously and consider alternative approaches when possible.
Conclusion and Call to Action 📢
By leveraging the power of Reflection, we have successfully addressed the problem of retrieving a property value based on its name. This solution can be particularly useful when working with dynamic scenarios or situations where you need to access properties dynamically.
I hope you found this post helpful! If you have any questions, suggestions, or alternative solutions, feel free to leave a comment below. Happy coding! 💻😄
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



