186

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;
}

10 Answers 10

368
return car.GetType().GetProperty(propertyName).GetValue(car, null);
4
  • 25
    Keep in mind that since this uses reflection, it is much slower. Probably not an issue, but good to be aware of.
    – Matt Greer
    Apr 1, 2011 at 0:48
  • "Cannot convert from String to BindingFlags"
    – Christine
    Jul 1, 2016 at 22:46
  • 13
    Is there a "faster" way @MattGreer?
    – FizxMike
    Aug 24, 2017 at 15:17
  • @FizxMike faster way
    – zwcloud
    Mar 13 at 11:49
55

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");
3
  • can I do this also for a SetValue? how? May 22, 2015 at 9:47
  • 3
    minor thing- extension method probably doesn't need to have the variable named car
    – KyleMit
    Feb 10, 2018 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
    – nwsmith
    Aug 1, 2019 at 17:32
44

You want Reflection

Type t = typeof(Car);
PropertyInfo prop = t.GetProperty("Make");
if(null != prop)
return prop.GetValue(this, null);
3
  • 9
    +1 This is the best answer as you're showing all the intermediate objects
    – Matt Greer
    Apr 1, 2011 at 0:52
  • 2
    Instead of passing property value can I pass index and get the property name and value ( so I can loop through all the property)?
    – singhswat
    Oct 4, 2018 at 11:03
  • 1
    @singhswat You should ask that as a new question. Oct 5, 2018 at 15:55
11

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.

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");
1
  • 4
    As of C#6 you can use null propagation return T.GetType().GetProperty(PropName)?.GetValue(T, null); Sep 1, 2020 at 21:01
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"));
    }
3

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.

1
  • 1
    Can you give an example?
    – Kris
    Jun 24, 2022 at 7:50
1

2 Very short options, 1 with a default value if it fails:

public object GetPropertyValue_WithDefault(
    object _t,
    string _prop,
    object _default = null
)
{
    PropertyInfo pi = _t.GetType().GetProperty(_prop);
    return (pi == null
        ? _default
        : pi.GetValue(_t, null)
    );
}

public object GetPropertyValue(object _t, string _prop)
{
    //because of "?." will return null if property not found
    return _t.GetType().GetProperty(_prop)?.GetValue(_t, null); 
}
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 1 at 2:05
1

Here is a faster way by creating your own customized RTTI.

public class Car
{
    public string MakeA { get; set; }
    public string MakeB { get; set; }
    public string MakeC { get; set; }

    private readonly Dictionary<string, Func<string>> _stringPropertyMap;
    //For properties of other types, add different typed Dictionary like _stringPropertyMap

    public Car()
    {
        _stringPropertyMap = new Dictionary<string, Func<string>>(3)
        {
            { nameof(MakeA), () => MakeA },
            { nameof(MakeB), () => MakeB },
            { nameof(MakeC), () => MakeC }
        };
    }

    public string GetPropertyValue(string propertyName)
    {
        if (_stringPropertyMap.TryGetValue(propertyName, out var getter))
        {
            return getter();
        }

        throw new InvalidOperationException($"{nameof(Car)} doesn't have a property of name {propertyName}");
    }
}
1
  • This only works if all of your properties are strings, which is unlikely. It would need to be generic and return any property.
    – HackSlash
    Jul 21 at 17:24
0

Expanding upon the answer given by @zwcloud, we can use any property type if you are using a version of .NET that supports the dynamic type.

Then if you need to dynamically load settings into a property you only have the string name of, you need a separate setter dictionary.

Now we arrive at a solution where we can get or set any property by name. It does take a lot of ceremony to get here but it ends up being much better than reflection.

To make an extension out of this we would need to get into code weaving, where this code could be generated for you at compile time. Tune in next time for another exciting episode of "ain't nobody got time for that".

public class Car
{
    public string MakeA { get; set; }
    public int MakeB { get; set; }
    public decimal MakeC { get; set; }

    private readonly Dictionary<string, Func<dynamic>> _propertyGetterMap;
    
    private readonly Dictionary<string, Action<dynamic>> _propertySetterMap;

    public Car()
    {
        _propertyGetterMap= new Dictionary<string, Func<dynamic>>(3)
        {
            { nameof(MakeA), () => MakeA },
            { nameof(MakeB), () => MakeB },
            { nameof(MakeC), () => MakeC }
        };

        _propertySetterMap= new Dictionary<string, Func<dynamic>>(3)
        {
            { nameof(MakeA), (value) => MakeA = value.ToString() },
            { nameof(MakeB), (value) => MakeB = (int)Convert.ChangeType(value, typeof(int)) },
            { nameof(MakeC), (value) => MakeC = (decimal)Convert.ChangeType(value, typeof(decimal)) }
        };
    }

    public string GetPropertyValue(string propertyName)
    {
        if (_stringPropertyMap.TryGetValue(propertyName, out var getter))
        {
            return getter();
        }

        throw new InvalidOperationException($"{nameof(Car)} doesn't have a property of name {propertyName}");
    }

    public string SetPropertyValue(string propertyName, dynamic value)
    {
        if (_stringPropertyMap.TryGetValue(propertyName, out var setter))
        {
            setter(value);
        }

        throw new InvalidOperationException($"{nameof(Car)} doesn't have a property of name {propertyName}");
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.