How to get a property value based on the name

get all properties and values of an object c#
c# set property value by name
c# reflection get property value generic list
reflection get property value java
get value of a property
c# reflection set property value
c# reflection get all properties of type
get property value through reflection

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

return car.GetType().GetProperty(propertyName).GetValue(car, null);

PropertyInfo.GetValue Method (System.Reflection), The string containing the name of the public property to get. Static (in Visual Basic, combine the values using Or ) to get it. following table shows what members of a base class are returned by the Get methods when reflecting on a type. is there a way to get the value of a property of a object based on its name? I want to write a method where I can pass in the property name and it would return the property value. ie: 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 '11 at 0:48.


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");

Type.GetProperty Method (System), GetProperties(); //Now we will loop in all properties one by one to get value foreach (PropertyInfo property in arrayPropertyInfos) { Console.WriteLine("Name of  Examples. The following example defines a Planet class that has two properties: Name, the name of the planet; and Distance, the planet's distance from Earth.The example instantiates a Planet object that represents the planet Jupiter and passes it to a GetPropertyValues method that displays information about the properties and uses the GetValue method to get the value of each Planet property.


You want Reflection

Type t = typeof(Car);
PropertyInfo prop = t.GetProperty("Make");
if(null != prop)
return prop.GetValue(this, null);

Using System.Reflection to find Property Name and Value of any , Get a property value given its name : PropertyInfo « Reflection « C# / C Sharp. How comps determine home value. In order to determine a home’s value using comps, three to five comps are collected and grouped together. Then, a report is generated determining a market value, based on the sale prices and details of these homes. You could get two types of reports, based on who is doing the calculations:


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

Get a property value given its name : PropertyInfo « Reflection « C# , Setting and Getting Property Values ¶ Example #1 Access via property name The simplest form of this uses the property name as the array index. We can use XPath to query and identify parts of a data object based on instance data. Can I set a property value with Reflection? How do I set a static property of a class using reflection when I only have its string name of the property? For instance I have: I don't know how I should set the value of the property using the property name string. Everything is dynamic. I could be setting 5 static properties of a class using 5


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.

Setting and Getting Property Values - Manual, Get the value of a property for the first element in the set of matched elements or set one or more The name of the property to get. Before jQuery 1.6, the .attr() method sometimes took property values into account By using a function to set properties, you can compute the value based on other properties of the element. Java get property value by property name. Ask Question Asked 6 years, 8 months ago. You can use some of the Libraries that offer property-based access.


.prop(), The name of the attribute to get. Using jQuery's .attr() method to get the value of an element's attribute has two main benefits: By using a function to set attributes, you can compute the value based on other properties of the element. In addition to the overall value, you also get estimated price per square foot, property details, sales history and value history. Our Home Value Estimator can be a great first step in determining how much a property is really worth. Note that there are many factors that may have an effect on home value.


.attr(), MATLAB® calls a property's get method whenever the property value is queried. have the following syntax, where PropertyName is the name of the property. Area method calculates the value based on the Width and Height properties. The Zestimate home valuation is Zillow’s estimated market value for a home, computed using a proprietary formula. It is a starting point in determining a home’s value and is not an official appraisal. The Zestimate is calculated from public and user-submitted data. Updating your home facts can help make your Zestimate more accurate.


Property Get Methods - MATLAB & Simulink, Dynamically calculates a value based on other properties. Element { static get properties() { return { user: String, isHappy: Boolean, count: { type: Number, readOnly: true, notify: Attribute names are converted to lowercase property names. Determine the number of vacant units, if any, of the property you want to value. In the example, use two vacant units. Determine the monthly market rental rate per unit of the vacant units. You can estimate this by using an amount similar to current rental listings of similar units in the area.