177

is there a way to get the value of a property of a object based on its name?

For example if I have:

public class Car : Vehicle
{
   public string Make { get; set; }
}

and

var car = new Car { Make="Ford" };

I want to write a method where I can pass in the property name and it would return the property value. ie:

public string GetPropertyValue(string propertyName)
{
   return the value of the property;
}
Coder 2
  • 4,661
  • 12
  • 37
  • 43

7 Answers7

355
return car.GetType().GetProperty(propertyName).GetValue(car, null);
Matt Greer
  • 59,108
  • 17
  • 121
  • 123
52

You'd have to use reflection

public object GetPropertyValue(object car, string propertyName)
{
   return car.GetType().GetProperties()
      .Single(pi => pi.Name == propertyName)
      .GetValue(car, null);
}

If you want to be really fancy, you could make it an extension method:

public static object GetPropertyValue(this object car, string propertyName)
{
   return car.GetType().GetProperties()
      .Single(pi => pi.Name == propertyName)
      .GetValue(car, null);
}

And then:

string makeValue = (string)car.GetPropertyValue("Make");
Adam Rackis
  • 81,646
  • 52
  • 262
  • 388
  • can I do this also for a SetValue? how? – Piero Alberto May 22 '15 at 09:47
  • 2
    minor thing- extension method probably doesn't need to have the variable named `car` – KyleMit Feb 10 '18 at 23:49
  • To see how to Set the property value, based on a propertyName string, see the answer here: [Setting the value of properties via reflection](https://codereview.stackexchange.com/questions/102289/setting-the-value-of-properties-via-reflection) – nwsmith Aug 01 '19 at 17:32
40

You want Reflection

Type t = typeof(Car);
PropertyInfo prop = t.GetProperty("Make");
if(null != prop)
return prop.GetValue(this, null);
Chuck Savage
  • 11,491
  • 6
  • 47
  • 67
8

In addition other guys answer, its Easy to get property value of any object by use Extension method like:

public static class Helper
    {
        public static object GetPropertyValue(this object T, string PropName)
        {
            return T.GetType().GetProperty(PropName) == null ? null : T.GetType().GetProperty(PropName).GetValue(T, null);
        }

    }

Usage is:

Car foo = new Car();
var balbal = foo.GetPropertyValue("Make");
Ali
  • 3,213
  • 4
  • 37
  • 53
8

Expanding on Adam Rackis's answer - we can make the extension method generic simply like this:

public static TResult GetPropertyValue<TResult>(this object t, string propertyName)
{
    object val = t.GetType().GetProperties().Single(pi => pi.Name == propertyName).GetValue(t, null);
    return (TResult)val;
}

You can throw some error handling around that too if you like.

Cameron Forward
  • 622
  • 7
  • 12
7

Simple sample (without write reflection hard code in the client)

class Customer
{
    public string CustomerName { get; set; }
    public string Address { get; set; }
    // approach here
    public string GetPropertyValue(string propertyName)
    {
        try
        {
            return this.GetType().GetProperty(propertyName).GetValue(this, null) as string;
        }
        catch { return null; }
    }
}
//use sample
static void Main(string[] args)
    {
        var customer = new Customer { CustomerName = "Harvey Triana", Address = "Something..." };
        Console.WriteLine(customer.GetPropertyValue("CustomerName"));
    }
Sith2021
  • 2,584
  • 25
  • 22
2

To avoid reflection you could set up a Dictionary with your propery names as keys and functions in the dictionary value part that return the corresponding values from the properties that you request.

Paul McCarthy
  • 722
  • 9
  • 19