C# get property of object code example

Example 1: c# get type of object

//Exact runtime type of the current instance.
object.GetType();

Example 2: c# get property using string

public static object GetPropValue(object src, string propName)
 {
     return src.GetType().GetProperty(propName).GetValue(src, null);
 }

Example 3: c# get object property value by name

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

Example 4: access object property C#

System.Reflection.PropertyInfo prop = typeof(YourType).GetProperty("PropertyName");

object value = prop.GetValue(yourInstance);
...
prop.SetValue(yourInstance, "value");

Example 5: get key value from object c#

Type myType = myObject.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

foreach (PropertyInfo prop in props)
{
    object propValue = prop.GetValue(myObject, null);

    // Do something with propValue
}